第二章 基本线程编程编程
1.(P25)若是多个线程等待同一个线程终止,则全部等待线程将一直等到目标线程终止。而后,一个等待线程成功返回,其他的等待线程将失败并返回ESRCH错误。函数
2.(P26)将新线程的pbe参数做为栈参数进行传递。这个线程参数之因此可以做为栈参数传递,是由于主线程会等待辅助线程终止。不过,首选方法是使用malloc从堆分配存储,而不是传递指向线程栈存储的地址。若是将该参数做为地址传递到线程栈存储,则该地址可能无效或者在线程终止时会被从新分配。spa
3.(P28)pthread_detach(3C)是pthread_join()的替代函数,可回收建立时detachstate属性设置为PTHREAD_CREATE_JOINABLE的线程的存储空间。线程
int pthread_detach(thread_t tid);
4.(P36)若是tid1和tid2相等,pthread_equal()将返回非零值,不然将返回零。若是tid1或tid2是无效的线程标识号,则结果没法预测。code
int pthread_equal(pthread_t tid1, pthread_t tid2);
5.(P37)使用sched_yield(3RT),可使当前线程中止执行,以便执行另外一个具备相同或更高优先级的线程。blog
int sched_yield(void);
6.(P38)请使用pthread_setschedparam()修改现有线程的优先级。此函数对于调度策略不起做用。pthread_getschedparam(3C)可用来获取现有线程的优先级。进程
int pthread_setschedparam(pthread_t tid, int policy,const struct sched_param *param); int pthread_getschedparam(pthread_t tid, int policy,struct schedparam *param);
#include <pthread.h>
pthread_t tid; int ret; struct sched_param param; int priority;
/* sched_priority will be the priority of the thread */ param.sched_priority = priority; policy= SCHED_OTHER;
/* scheduling parameters of target thread */ ret = pthread_setschedparam(tid,policy,¶m);
7.(P40)请使用pthread_kill()向线程发送信号。sig参数必须来自signal(5)提供的列表。若是sig为零,将执行错误检查,但并不实际发送信号。此错误检查可用来检查tid的有效性。get
int pthread_kill(thread_t tid, int sig);
8.(P43)当初始线程(即调用main()的线程)从main()调用返回时或调用exit()时,整个进程及其全部的线程将终止。若是主线程仅仅调用了pthread_exit,则仅主线程自己终止。进程及进程内的其余线程将继续存在。全部线程都已终止时,进程也将终止。it