epoll_wait是線(xiàn)程退出點(diǎn)嗎?
2010年04月02日 星期五 上午 10:46
在Linux上,epoll_wait是否cancellation point,取決于glibc的版本. 好像在glibc-2.4以上版本中才支持. 假如系統(tǒng)epoll_wait不是cancellation point,那么在某線(xiàn)程執(zhí)行epoll_wait時(shí),另一線(xiàn)程通過(guò)pthread_cancel發(fā)出cancel信號(hào),那么表現(xiàn)為: 1. 在epoll_wait返回之前,線(xiàn)程阻塞該信號(hào),并不響應(yīng); 2. 當(dāng)epoll_wait返回時(shí),線(xiàn)程立即退出(所有資源不會(huì)被釋放),在此之前即使通過(guò)pthread_cleanup_push注冊(cè)了銷(xiāo)毀時(shí)的回調(diào)函數(shù)也不會(huì)被執(zhí)行;同時(shí),如果有線(xiàn)程阻塞在pthread_join等待此線(xiàn)程退出的話(huà),則會(huì)返回. 測(cè)試代碼如下:
/* test epoll_wait is a cancellation point. glibc-2.4 or later support.
fedora 5 support. */
#include <sys/epoll.h> #include <pthread.h> #include <stdio.h> #include <unistd.h>
void * run (int * events) { struct epoll_event storage;
printf("Wait start.\n"); /* block forever */ epoll_wait(*events, &storage, 1, -1/*10000*/);
printf("Wait stop.\n");
return NULL; }
int main (void) { int events; pthread_t new_thread;
events = epoll_create(1); pthread_create(&new_thread, NULL, (void * (*) (void *))run, &events);
/* wait for enter keypress to try pthread cancellation */ getchar(); printf("Cleanup start.\n"); pthread_cancel(new_thread);
/* let's see if this returns or not */ pthread_join(new_thread, NULL);
/* if to here, the epoll_wait is a cancellation point. */ printf("Cleanup stop.\n");
close(events); return 0; }
|
|
|