pthread_exit

王朝百科·作者佚名  2010-10-02  
宽屏版  字体:   |    |    |  超大  

pthread_exit(void* retval);

线程通过调用pthread_exit函数终止执行,就如同进程在结束时调用exit函数一样。这个函数的作用是,终止调用它的线程并返回一个指向某个对象的指针。

pthread_exit() 参数

#include #include #include

void *print_message_function( void *ptr )

{

char *message;

message = (char *) ptr;

printf("%s ", 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).

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝百科 版权所有