此范例demo如何在Linux下建立一个thread。
1 /**/ /* 2(C) OOMusou 2006 http://oomusou.cnblogs.com 3 4Filename : pthread_create.cpp 5Compiler : gcc 4.10 on Fedora 5 / gcc 3.4 on Cygwin 1.5.21 6Description : Demo how to create thread in Linux. 7Release : 12/03/2006 8Compile : g++ -lpthread pthread_create.cpp 9*/ 10 #include < stdio.h > 11 #include < stdlib.h > // exit(), EXIT_SUCCESS 12 #include < pthread.h > // pthread_create(),pthread_join() 13 14 void * helloWorld( void * arg); 15 16 int main() { 17 // Result for System call18 int res = 0;1920 // Create thread21 pthread_t thdHelloWorld; 22 res = pthread_create(&thdHelloWorld, NULL, helloWorld, NULL);23 if (res) { 24 printf("Thread creation failed!!\n");25 exit(EXIT_FAILURE);26 }2728 // Wait for thread synchronization29 void *threadResult;30 res = pthread_join(thdHelloWorld, &threadResult);31 if (res) { 32 printf("Thread join failed!!\n");33 exit(EXIT_FAILURE);34 }3536 exit(EXIT_SUCCESS);37} 38 39 void * helloWorld( void * arg) { 40 printf("Hello World\n");41}