分类:
2008-12-16 10:17:32
12 thread control
12.3 thread attributes
Thread的一些属性可以被设置:
1.Stack
的位置
2. stack的大小
3.是否是detach
state
Detach state的thread在结束后,系统就将其storage给reclaim了,而不用别人调用pthread_join去给他收尸。所以对于不需要获取thread的exit status的情况下可以使用。一个线程可以通过调用pthread_detach来将自己的状态改为detached。我们也可以将一个线程建立之前就给其加入detach属性。
4. guard size,即在一个线程的stack的结尾可以加入一段guard区域,当发生stack overflow时,访问到该区域的时候,该线程会收到signal。
还有些附加的属性:
1.Thread
cancelability state
2. thread cancelability type
3. concurrency level
所谓的concurrency level对于采用将多个user thread分配到多个kernel thread的thread s model来说是有好处的,因为提高concurrency可以增加N:M中的M,即让更多的kenel thread来完成N个thread的工作,使其更具有被调度的可能。但对于采用1: 1模型的linux来说没啥意义,因为linux里,application建立一个thread,就对应了一个kernel thread。
如下是各平台的属性支持表:
Figure 12.3. POSIX.1 thread attributes |
|||||
Name |
Description |
FreeBSD |
Linux |
Mac OS X 10.3 |
Solaris 9 |
detachstate |
detached thread attribute |
• |
• |
• |
• |
guardsize |
guard buffer size in bytes at end of thread stack |
|
• |
• |
• |
stackaddr |
lowest address of thread stack |
ob |
• |
• |
ob |
stacksize |
size in bytes of thread stack |
• |
• |
• |
• |
#include int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *attr); |
Both return: 0 if OK, error number on failure |
初始化及释放属性结构体。
#include int pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int
*detachstate); int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate); |
Both return: 0 if OK, error number on failure |
#include int pthread_attr_getstack(const pthread_attr_t *restrict attr, void **restrict
stackaddr, size_t *restrict
stacksize); int pthread_attr_setstack(const pthread_attr_t *attr, void *stackaddr,
size_t *stacksize); |
Both return: 0 if OK, error number on failure |
#include int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t
*restrict stacksize); int pthread_attr_setstacksize(pthread_attr_t *attr , size_t stacksize); |
Both return: 0 if OK, error number on failure |
#include int pthread_attr_getguardsize(const pthread_attr_t *restrict attr, size_t *restrict guardsize); int pthread_attr_setguardsize(pthread_attr_t *attr , size_t guardsize); |
Both return: 0 if OK, error number on failure |
#include int pthread_getconcurrency(void); |
Returns: current concurrency level |
int pthread_setconcurrency(int level); |
Returns: 0 if OK, error number on failure |