pthread_create—pthread_cancel—pthread_join
点击(此处)折叠或打开
-
// gcc -lpthread server.c -o server
-
// indent -npro -kr -i8 -ts8 -sob -l280 -ss -ncs -cp1 *
-
#include <pthread.h>
-
#include <stdio.h>
-
#include <unistd.h>
-
#include <poll.h>
-
-
void *thread(void *arg)
-
{
-
int i = 0;
-
do {
-
struct pollfd f;
-
f.fd = 0;
-
f.events = POLLPRI;
-
poll(&f, 1, 10);
-
i++;
-
} while (i < 100);
-
return NULL;
-
}
-
-
#define CNT 5
-
-
int main()
-
{
-
void *res;
-
int i;
-
pthread_t t[CNT];
-
do {
-
printf("New\n");
-
for (i = 0; i < CNT; i++)
-
pthread_create(&t[i], NULL, thread, 0);
-
printf("Stop\n");
-
for (i = 0; i < CNT; i++) {
-
pthread_cancel(t[i]);
-
pthread_join(t[i], &res);
-
pthread_create(&t[i], NULL, thread, 0);
-
}
-
} while (1);
-
return 0;
- }
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(37) | 评论(0) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
评论热议

浙公网安备 33010602011771号