线程创建
线程退出
资源回收
线程属性
线程有自身的属性,属性类型为:pthread_attr_t,在使用pthread_create()接口创建线程时,可以通过设置线程属性对象的值,并作为创建线程的入参来设置新创建线程的属性。
在使用一个线程属性对象之前必须进行初始化,在使用后需要对其去除初始化。
pthread_attr_init()接口
调用pthread_attr_init之后,pthread_t结构所包含的内容就是操作系统实现支持的线程所有属性的默认值。
如果不想使用系统设置的默认线程属性值,可以调用pthread_attr_setxxx()系列接口设置自定义的值,例如:
使用pthread_attr_setdetachstate();来进行设置线程的分离状态。
使用pthread_attr_setscope();来进行设置线程的绑定状态。
使用pthread_attr_setstacksize;来设置创建新的线程时,系统分配的堆栈大小;如果不指定,创建线程时,系统将会使用默认值
查看默认值方法如下:
# ulimit -s
8192
#
上述表示为8M;单位为KB。
一般来说 默认堆栈大小为 8388608; 堆栈最小为 16384 。 单位为字节。
堆栈最小值定义为 PTHREAD_STACK_MIN ,包含#include <limits.h>后可以通过打印其值查看。对于默认值可以通过pthread_attr_getstacksize (&attr, &stack_size); 打印stack_size来查看。
尤其在嵌入式中内存不是很大,若采用默认值的话,会导致出现问题,若内存不足,则 pthread_create 会返回 12,定义如下:
#define ENOMEM 12 /* Out of memory */
一般在开辟新的线程时,新线程对资源要求不是特别高时就使用此接口设置自定义的堆栈值。
pthread_attr_destory()接口
如果要去除对pthread_attr_t结构的初始化,可以调用pthread_attr_destroy函数。如果pthread_attr_init实现时为属性对象分配了动态内存空间,pthread_attr_destroy还会释放动态内存,用无效的值初始化属性对象,因此如果经pthread_attr_destroy去除初始化之后的pthread_attr_t结构被pthread_create函数调用,将会导致其返回错误。man手册有介绍:
POSIX 标准要求:
When a thread attributes object is no longer required, it should be destroyed using the pthread_attr_destroy() function. Destroying a thread attributes object has no effect on threads that were created using that object.
当不再需要线程属性对象时,应该使用pthread_attr_destroy()函数销毁它。销毁线程属性对象对使用该对象创建的线程没有影响。
Once a thread attributes object has been destroyed, it can be reinitialized using pthread_attr_init(). Any othrer use of a destoryed thread attributes object has undefined results.
一旦线程属性对象被销毁,就可以使用它重新初始化pthread_attr_init()。任何对破坏线程属性对象的使用都有未定义的结果。
线程分离