2007年7月31日星期二

主进程中如何退出pthread_create创建的线程?

cancel有两种模式,Defered或者Asynchronous,
Defered模式线程只在某些cancel点检查自己是否需要cancel,
Async模式则是立即cancel。
当然两种模式下,通过pthread_cleanup_push()建立的cleanup还是会执行的。

一个Async模式的例子。


#include

#include



#include



static pthread_t child;



static void fatal(const char *fmt, ...)

{

va_list arglist;



fprintf(stderr, "FATAL: ");



va_start(arglist, fmt);

vfprintf(stderr, fmt, arglist);

fputs("\n", stderr);

va_end(arglist);



exit(1);

}



static void say(const char *fmt, ...)

{

va_list arglist;



va_start(arglist, fmt);

vfprintf(stderr, fmt, arglist);

fputs("\n", stderr);

va_end(arglist);

}



static void child_cleanup(void *arg)

{

say(" [-child-] Cleanup routine called");

}



static void *thread_func(void *arg)

{

int i=0;



if(pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL))

fatal("set cancel type failed");



pthread_cleanup_push(&child_cleanup, NULL);



say(" [-child-] Entering dead loop...");

while(1){

i++;

}



pthread_cleanup_pop(0);



return NULL;

}



int main()

{

void *pe = NULL;



say("Creating child thread...");

if(pthread_create(&child, NULL, thread_func, NULL))

fatal("Failed to create a thread");



/* Wait for child to set cancel type */

/* Use pthread_barrier_wait() to guarantee that.*/

sleep(3);



say("Cancelling child...");

if(pthread_cancel(child))

fatal("Failed to cancel child thread");



say("Joining child...");

if(pthread_join(child, &pe))

fatal("Failed to join the child thread");



if(pe == PTHREAD_CANCELED){

say("Child is really Canceled");

}



say("Main thread exiting...");

return 0;

}

没有评论: