Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sun(unix) + Gcc + thread

Status
Not open for further replies.

jbpelletier

Programmer
Sep 8, 2001
232
0
0
CA
On Sun/unix using gcc

this is an exemple i found in the man of thr_create.. but it dont work??? well when i run the executable.. the
prinft("..."); located in the thread never appear on screen.

this is the code :
#define _REENTRANT
#include <stdio.h>
#include <stdlib.h>
#include <thread.h>

/* function prototypes and global varaibles */
void *thr_sub(void *);
mutex_t lock;

main(int argc, char **argv)
{
int i, thr_count = 100;
char buf;

/* check to see if user passed an argument
-- if so, set the number of threads to the value
passed to the program */

if (argc == 2) thr_count = atoi(argv[1]);

printf(&quot;Creating %d threads...\n&quot;, thr_count);

/* lock the mutex variable -- this mutex is being used to
keep all the other threads created from proceeding */

mutex_lock(&lock);

/* create all the threads -- Note that a specific stack size is
given. Since the created threads will not use all of the
default stack size, we can save memory by reducing the threads'
stack size */

for (i=0;i<thr_count;i++) {
thr_create(NULL,2048,thr_sub,0,0,NULL);
}

printf(&quot;%d threads have been created and are running!\n&quot;, i);
printf(&quot;Press <return> to join all the threads...\n&quot;, i);

/* wait till user presses return, then join all the threads */
gets(&buf);

printf(&quot;Joining %d threads...\n&quot;, thr_count);

/* now unlock the mutex variable, to let all the threads proceed */
mutex_unlock(&lock);

/* join the threads */
for (i=0;i<thr_count;i++)
thr_join(0,0,0);

printf(&quot;All %d threads have been joined, exiting...\n&quot;, thr_count);
return(0);
}

/* The routine that is executed by the created threads */
void *thr_sub(void *arg)
{
/* try to lock the mutex variable -- since the main thread has
locked the mutex before the threads were created, this thread
will block until the main thread unlock the mutex */

mutex_lock(&lock);

printf(&quot;Thread %d is exiting...\n&quot;, thr_self());

/* unlock the mutex to allow another thread to proceed */
mutex_unlock(&lock);
/* exit the thread */
return((void *)0);
}



my questions are..
do there is errors in that code?
what should be the console line to compile it (actualy i tried : gcc -Wall test.c -o test
and
gcc test.c -lthread

but still dont work

tanx
jb
 
u have locked the mutex in the main function ...
so when the thread gets executed... it waits at the mutex..
so the printf statement never gets executed

cvs
 
na the mutex lock is there only to &quot;start&quot; all the thread at the same times.. when unlock heppen a couple of line after

in fact my problem was resolve using
gcc test.c -o test -lthread
instead of:
gcc test.c -lthread

tanx anyway
jb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top