jbpelletier
Programmer
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("Creating %d threads...\n", 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("%d threads have been created and are running!\n", i);
printf("Press <return> to join all the threads...\n", i);
/* wait till user presses return, then join all the threads */
gets(&buf);
printf("Joining %d threads...\n", 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("All %d threads have been joined, exiting...\n", 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("Thread %d is exiting...\n", 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
this is an exemple i found in the man of thr_create.. but it dont work??? well when i run the executable.. the
prinft("..."
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("Creating %d threads...\n", 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("%d threads have been created and are running!\n", i);
printf("Press <return> to join all the threads...\n", i);
/* wait till user presses return, then join all the threads */
gets(&buf);
printf("Joining %d threads...\n", 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("All %d threads have been joined, exiting...\n", 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("Thread %d is exiting...\n", 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