《Linux多线程编程手册》读书笔记

第二章 基本线程编程

1.(P25)如果多个线程等待同一个线程终止,则所有等待线程将一直等到目标线程终止。然后,一个等待线程成功返回,其余的等待线程将失败并返回ESRCH错误

2.(P26)将新线程的pbe参数作为栈参数进行传递。这个线程参数之所以能够作为栈参数传递,是因为主线程会等待辅助线程终止。不过,首选方法是使用malloc从堆分配存储,而不是传递指向线程栈存储的地址。如果将该参数作为地址传递到线程栈存储,则该地址可能无效或者在线程终止时会被重新分配。

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是无效的线程标识号,则结果无法预测。

int pthread_equal(pthread_t tid1, pthread_t tid2);

5.(P37)使用sched_yield(3RT),可以使当前线程停止执行,以便执行另一个具有相同或更高优先级的线程。

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,&param);

7.(P40)请使用pthread_kill()向线程发送信号。sig参数必须来自signal(5)提供的列表。如果sig为零,将执行错误检查,但并不实际发送信号。此错误检查可用来检查tid的有效性。

int pthread_kill(thread_t tid, int sig);

8.(P43)当初始线程(即调用main()的线程)从main()调用返回时或调用exit()时,整个进程及其所有的线程将终止。如果主线程仅仅调用了pthread_exit,则仅主线程本身终止。进程及进程内的其他线程将继续存在。所有线程都已终止时,进程也将终止。

原文地址:https://www.cnblogs.com/big-xuyue/p/3773628.html