create a thread from a returning function

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\);

}

int createThd()
{
int rc = 0;
pthread_t mythread;

if \( pthread_create\( &mythread, NULL, thread_function, NULL\) \) \{
	printf\("error creating thread."\);
	return\(-1\);
\}
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.

HTH.

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...

thankx ramen_noodle & andryk :b:
ramen_noodle was actually correct, sorry abt my lack of knowledge.
i put a loop in my main program and it was ok.
wolwy.