博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux线程(一)
阅读量:4663 次
发布时间:2019-06-09

本文共 2706 字,大约阅读时间需要 9 分钟。

线程的优先级无法保障线程的执行次序。只不过优先级高的线程获取 CPU 资源的概率大一点而已。

一. pthread_create()

#include <pthread.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

pthread_t *thread:           传递一个pthread_t变量地址进来,用于保存新线程的tid(线程ID)

const pthread_attr_t *attr:      线程属性设置,如使用默认属性,则传NULL
void *(*start_routine) (void *):      
函数指针,指向新线程应该加载执行的函数模块
void *arg:            
指定线程将要加载调用的那个函数的参数

返回值:成功返回0,失败返回错误号。以前学过的系统函数都是成功返回0,失败返回-1,而错误号保存在全局变量errno中,而pthread库的函数都是通过返回值返回错误号,虽然每个线程也都有一个errno,但这是为了兼容其它函数接口而提供的,pthread库本身并不使用它,通过返回值返回错误码更加清晰。

 

二.pthread_join()

#include <pthread.h>

int pthread_join(pthread_t thread, void **retval);

pthread_t thread:        回收线程的tid
void **retval:          接收退出线程传递出的返回值
返回值:成功返回0,失败返回错误号

注意:

调用该函数的线程将挂起等待,直到id为thread的线程终止。thread线程以不同的方法终止,通过pthread_join得到的终止状态是不同的,总结如下:

如果thread线程通过return返回,retval所指向的单元里存放的是thread线程函数的返回值。
如果thread线程被别的线程调用pthread_cancel异常终止掉,retval所指向的单元里存放的是常数PTHREAD_CANCELED。
如果thread线程是自己调用pthread_exit终止的,retval所指向的单元存放的是传给pthread_exit的参数。
如果对thread线程的终止状态不感兴趣,可以传NULL给retval参数。

三.pthread_exit()

#include <pthread.h>

void pthread_exit(void *retval);
void *retval:      线程退出时传递出的参数,可以是退出值或地址,如是地址时,不能是线程内部申请的局部地址。

注意和exit函数的区别,任何线程里exit导致进程退出,其他线程未工作结束,主线程退出时不能return或exit。需要注意,pthread_exit或者return返回的指针所指向的内存单元必须是全局的或者是用malloc分配的,不能在线程函数的栈上分配,因为当其它线程得到这个返回指针时线程函数已经退出了。

四.pthread_cancel()

#include <pthread.h>

int pthread_cancel(pthread_t thread);

定义在Linux的pthread库中常数PTHREAD_CANCELED的值是-1。可以在头文件pthread.h中找到它的定义:

#define PTHREAD_CANCELED ((void *) -1)

五.示例

#include 
#include
#include
void *thr_fn1(void *arg){ printf("thread 1 returning\n"); return (void*)1;}void *thr_fn2(void *arg){ printf("thread 2 exiting\n"); pthread_exit((void*)2);}void *thr_fn3(void *arg){ while(1) { printf("thread 3 writing\n"); sleep(1); }}int main(){ pthread_t tid; void *retval; pthread_create(&tid, NULL, thr_fn1, NULL); pthread_join(tid, &retval); printf("thread 1 exit code %d\n", (int)retval); pthread_create(&tid, NULL, thr_fn2, NULL); pthread_join(tid, &retval); printf("thread 2 exit code %d\n", (int)retval); pthread_create(&tid, NULL, thr_fn3, NULL); sleep(3); // 调用pthread_cancel函数取消第三个线程 pthread_cancel(tid); // 如果线程是通过pthread_cancel异常终止掉,retval所指向的单元里存放的是常数PTHREAD_CANCELED pthread_join(tid, &retval); printf("thread 3 exit code %d\n", (int)retval); return 0;}

运行结果:

thread 1 returning

thread 1 exit code 1
thread 2 exiting
thread 2 exit code 2
thread 3 writing
thread 3 writing
thread 3 writing
thread 3 exit code -1

转载于:https://www.cnblogs.com/guxuanqing/p/10574689.html

你可能感兴趣的文章
Lua 5.2 Reference Manual
查看>>
Jenkins遇到问题一:jenkins配置权限不对导致无法登陆或者空白页面解决办法
查看>>
机器学习(十一)-------- 推荐系统(Recommender Systems)
查看>>
局部a链接样式
查看>>
Django-配置静态文件
查看>>
Spark内存管理详解
查看>>
插入排序
查看>>
Aliyun cdn访问日志下载
查看>>
[zz]Fedora 15 安装与配置一览
查看>>
有关委托的理解
查看>>
Yii2.0实用功能技巧解密之——分页功能
查看>>
开始撸凝聚普及赛咯
查看>>
thinkphp5 通用后台
查看>>
[转]Show parameter & Table Not exists
查看>>
Vue-router入门
查看>>
setTimeout与setInterval
查看>>
【转】C#详解值类型和引用类型区别
查看>>
(算法)游戏必胜策略
查看>>
SSH免密码登录快速配置方法
查看>>
矩形嵌套
查看>>