Linux多线程编程总结

1. pthread_join和pthread_detach理解

pthread_detach表示主线程不等待线程退出, 线程资源在线程退出时自动回收(锁,条件变量,信号量,文件描述符)

pthread_join:等待线程退出,并回收进程资源。

 

2.进程退出时,进程内的线程,全部自动退出(皮之不存毛将焉附?)

3. pthread_key_create :在线程内部,私有数据可以被各个函数访问。但他对其他线程是屏蔽的

 pthread_setspecific/pthread_getspecific

 

4. pthread_atfork函数: 多线程环境下,如果调用fork,可能导致子进程资源(锁/信号量/条件变量)处于不确定状态,prepare在创建子进程之前,在父进程中调用,parent在fork返回之前,在父进程中调用, child在子进程创建之后,执行子进程之前,在子进程中调用.

5.关于线程的cancel和cleanup_push/pop的理解. 可以使能和禁止线程的cancel功能

pthread_setcancelstate/pthread_getcancelstate,并且可以设置取消的type为延迟或异步,pthread_setcanceltype/

pthread_getcanceltype,在延迟类型中,需要运行到取消点时,线程才会被取消.异步时,线程被cancel的时间不定.

在程序设计中,可以通过函数pthread_testcancel设置取消点,

pthread_cleanup_push/pthread_cleanup_pop,这两个函数必须成对的出现. 设置线程退出或者取消时的清理函数。清理函数被调用的三种情况.

1. 在线程执行到pthread_cleanup_pop(0)之前,线程被pthread_cancel掉.

2.在线程执行到pthread_cleanup_pop(0)之前,线程调用pthread_exit()退出.

3.线程执行到pthread_cleanup_pop(1)时,立即执行清理函数.

几种不会执行清理函数的情况.

1.线程异常退出,如访问非法地址.

 2.   线程直接调用return返回。

3. 线程执行完pthread_cleanup_pop(0).

对函数pthread_cleanup_pop(execute)的理解:

 execute=1时,在调用pop函数时,立即调用清理函数

execute = 0时,在调用pop函数后,清理函数永远也不会被执行到.

 

6 pread/pwrite 在多线程环境下保证互斥的读写.

7. 互斥和条件变量

    pthread_mutex_init/pthread_mutex_lock/pthread_mutex_unlock .

    pthread_cond_init/pthread_cond_wait/pthread_cond_signal/broadcast

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值