当前位置:网站首页>unix环境编程学习-多线程
unix环境编程学习-多线程
2022-08-09 09:20:00 【y_dd】
线程创建、终止
创建
int pthread_create(pthread_t * tid , cosnt pthread_attr_t* attr, void *(start_rtn)(void), void * restrict arg)
tid为传出参数,新建线程的id
注意,新线程并不保证哪个线程会先运行,如果新建线程中使用tid不安全
终止
三种线程终止方式:
start_rtn所指向的线程函数返回,返回值是线程的退出码
pthread_exit,参数可以传结构体指针,也当然可以设置退出码
线程被取消
线程退出
void pthread_exit(void * ptr)
线程的退出码可以有pthread_join获取
int pthread_join(pthread_t thead, void **ptr) ptr可以获取退出码(和结构体)
注意:必须保证pthread_create和pthread_exit这两个函数void *的参数所使用的的内存,在使用者调用完成后依然有效。比如调用线程的栈上分配了内存,但是其他线程在使用的时候,已经发生了变更。比如pthread_exit的时候使用的内存分配在自己的线程栈里,但是别的线程在pthread_join使用的时候,该栈可能已经撤销,内存发生变更,无法获取退出状态
线程取消
int pthread_cancel(pthread_t tid)
线程清理程序
一个线程可建立多个线程清理程序
void pthread_cleanup_push(void (*pfun)(void *),
void pthread_cleanup_pop(int ex)
线程同步
互斥量
pthread_init
pthread_destroy
pthread_lock
pthread_unlock
注意避免死锁,当两个线程以不同顺序请求互斥量
pthread_mutex_timedlock 带有超时的互斥量,避免死锁
读写锁
适合对数据结构读大于写的情况
可以多个线程同时占有读模式的读写锁,只有一个线程可以占有写模式的读写锁
带有超时的读写锁
条件变量
与互斥量一起使用
边栏推荐
- 功能自动化测试实施的原则以及方法有哪些?
- 常用功能测试的检查点与用例设计思路
- 软件测试外包公司怎么样?有什么好处和坏处?为什么没人去?
- canal工作原理及简单案例演示
- Consolidation of Questionnaire Questions and Answers
- 多维度LSTM(长短期记忆)神经网络预测未来存款余额走势
- 学习双向链表的心得与总结
- MySQL Leak Check (4) Stored Procedures and Cursors
- 性能测试报告包括哪些内容?模板范文哪里找?看这里
- Domestic Google earth, terrain analysis seconds kill with the map software
猜你喜欢
随机推荐
Read file by byte and character_load configuration file
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
When and How to use MALLOC
Flutter的基础知识之Dart语法
这12个GIS软件一个比一个好用
游戏测试的概念是什么?测试方法和流程有哪些?
本体开发日记05-努力理解SWRL(Built-Ins)
进入大厂的面试经验(P7)
接口性能测试方案设计方法有哪些?要怎么去写?
Teach you how to get a 0.1-meter high-precision satellite map for free
【面试体系知识点总结】---JVM
数据治理(四):数据仓库数据质量管理
lateral view explode的另一种实现方式
黑盒测试常见错误类型说明及解决方法有哪些?
本体开发日记01-Jena配置环境变量
软件测试流程包括哪些内容?测试方法有哪些?
Consolidation of Questionnaire Questions and Answers
MySQL事务隔离
map去重代码实现
MySQL查漏补缺(五)不熟悉的知识点