词条 | pthread_exit |
释义 | void pthread_exit(void* retval); 线程通过调用pthread_exit函数终止执行,就如同进程在结束时调用exit函数一样。这个函数的作用是,终止调用它的线程并返回一个指向某个对象的指针。 pthread_exit() 参数 #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *print_message_function( void *ptr ) { char *message; message = (char *) ptr; printf("%s \\t", message); printf("PID: %ld \", pthread_self()); pthread_exit ("thread all done"); // 重点看 pthread_exit() 的参数,是一个字串,这个参数的指针可以通过 // pthread_join( thread1, &pth_join_ret1); } main() { pthread_t thread1, thread2; char *message1 = "Thread 1"; char *message2 = "Thread 2"; int iret1, iret2; void *pth_join_ret1;//="thread1 has done!"; void *pth_join_ret2;//="thread2 has done!"; /* Create independant threads each of which will execute function */ //pthread_create return 0 if create a thread is ok! iret1 = pthread_create( &thread1, NULL, print_message_function, (void*)"thread one_here"); iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2); pthread_join( thread1, &pth_join_ret1); pthread_join( thread2, &pth_join_ret2); // //if(pth_join_ret1==NULL || pth_join_ret2==NULL) //{ // printf("in %d lines \",__LINE__); //} printf("Thread 1 returns: %d\",iret1); printf("Thread 2 returns: %d\",iret2); printf("pthread_join 1 returns: %s\",(char *)pth_join_ret1); printf("pthread_join 2 returns: %s\",(char *)pth_join_ret2);/目的就是打印 线程退出时的返回值 exit(0); } //重点看上面红色注释。以前确实没注意,今天帮一个新同事调试程序才仔细看了说明 DESCRIPTION pthread_exit terminates the execution of the calling thread. All cleanup handlers that have been set for the calling thread with pthread_cleanup_push(3) are executed in reverse order (the most recently pushed handler is exe- cuted first). Finalization functions for thread-specific data are then called for all keys that have non- NULL val- ues associated with them in the calling thread (see pthread_key_create(3)). Finally, execution of the calling thread is stopped. The retval argument is the return value of the thread. It can be consulted from another thread using pthread_join(3). |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。