手机
当前位置:查字典教程网 >编程开发 >C语言 >c语言线程终止练习示例
c语言线程终止练习示例
摘要:复制代码代码如下:#include#include#includevoid*t1(void*args){return(void*)0;}vo...

复制代码 代码如下:

#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] = %dn", args);

pthread_exit((void *) 3);

}

void *t3(void *args) {

while(1) {

printf("thread 3 is workingn");

sleep(1);

}

}

int main(int argc, char *argv[]) {

pthread_t thread;

int err;

void *status;

printf("creating thread 1n");

err = pthread_create(&thread, NULL, t1, NULL);

if(err) {

printf("Can not created thread 1n");

exit(-1);

}

pthread_join(thread, &status);

printf("thread 1 exit return code %dnn", status);

printf("creating thread 2n");

err = pthread_create(&thread, NULL, t2, (void *) 9);

if(err) {

printf("Can not created thread 2n");

exit(-2);

}

pthread_join(thread, &status);

printf("thread 2 exit return code %dnn", status);

printf("creating thread 3n");

err = pthread_create(&thread, NULL, t3, NULL);

if(err) {

printf("Can not created thread 3n");

exit(-3);

}

sleep(10);

pthread_cancel(thread);

pthread_join(thread, &status);

printf("thread 3 exit return code %dn", status);

return 1;

}

【c语言线程终止练习示例】相关文章:

C语言解线性方程的四种方法

c语言中if 语句的作用范围示例代码

C语言数组指针的小例子

c++ dynamic_cast与static_cast使用方法示例

linux c多线程编程实例代码

C 语言restrict 关键字的使用浅谈

C语言运算符优先级列表(超详细)

c语言中十进制转二进制显示小工具的实现代码

C语言小程序 数组操作示例代码

C 语言基础教程(我的C之旅开始了)[五]

精品推荐
分类导航