hi all,
my requirement is to create a thread by calling another function.
i.e i dont call pthread_create directly from main, but by calling another function (createThd - below ), from main.
Example:
[CODE]
void *thread_function(void arg) { / thread function */
int i;
rc = pthread_detach(pthread_self());
for ( ; ; ) {
printf("Thread says hi!\n");
sleep(1);
}
pthread_exit(NULL);
}
int main(void) {
int i;
i = createThd\(\); /* call to thread creating function */
return \(0\);
}
[\CODE]
this creates the thread but doesnt run it, thread terminates as soon as the function createThd() returns.
is there a way to overcome this problem.
any help is appreciated, thankx in advanced.
wolwy.
The reason this is not working is that your main function doesn't have an event loop and/or doesn't block on the created thread.
An easy way out is to return the created thread tid, do not detach, and call pthread_join in main.
Hi,
You could wait on createThd() until thread_function() has done his job by means of pthread_cond_wait/pthread_cond_signal & mutexes, just like a parent process would wait (with wait/waitpid) for its child to terminate...