服务器之家

服务器之家 > 正文

c语言线程终止练习示例

时间:2021-01-19 13:54     来源/作者:C语言教程网

复制代码 代码如下:


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

 

void *t1(void *args) {
 return (void *) 0;
}

void *t2(void *args) {
 printf("thread 2 param[args] = %d\n", args);
 pthread_exit((void *) 3);
}

void *t3(void *args) {
 while(1) {
  printf("thread 3 is working\n");
  sleep(1);
 }
}

int main(int argc, char *argv[]) {
 pthread_t thread;
 int err;
 void *status;

 printf("creating thread 1\n");
 err = pthread_create(&thread, NULL, t1, NULL);
 if(err) {
  printf("Can not created thread 1\n");
  exit(-1);
 }
 pthread_join(thread, &status);
 printf("thread 1 exit return code %d\n\n", status);
 

 printf("creating thread 2\n");
 err = pthread_create(&thread, NULL, t2, (void *) 9);
 if(err) {
  printf("Can not created thread 2\n");
  exit(-2);
 }
 pthread_join(thread, &status);
 printf("thread 2 exit return code %d\n\n", status);

  
 printf("creating thread 3\n");
 err = pthread_create(&thread, NULL, t3, NULL);
 if(err) {
  printf("Can not created thread 3\n");
  exit(-3);
 }
 sleep(10);
 pthread_cancel(thread);
 pthread_join(thread, &status);
 printf("thread 3 exit return code %d\n", status);

 return 1;
}

 

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部