Reputation: 1274
void myThread(void *arg) {
printf("Thread ran!\n");
pthread_exit( NULL );
}
int main() {
int ret;
pthread_t mythread;
ret=pthread_create(&mythread,NULL,myThread,NULL);
if (ret != 0) {
printf( "Can’t create pthread (%s)\n", strerror(errno ) );
exit(-1);
}
return 0;
}
Upvotes: 2
Views: 199
Reputation: 5493
You have to wait in the main thread, use pthread_join()
after calling pthread_create()
.
Upvotes: 5
Reputation: 16728
Because main
returns immediately, before the thread has had a chance to execute - try adding sleep(1000);
before return 0;
and you'll probably find that it works.
If you'd like main
to wait until the thread finishes, try pthread_join (but then you might as well not have a thread at all).
pthread_join(mythread, 0);
return 0;
Upvotes: 9