Chinaunix首页 | 论坛 | 博客
  • 博客访问: 969031
  • 博文数量: 200
  • 博客积分: 5011
  • 博客等级: 大校
  • 技术积分: 2479
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 15:07
文章分类

全部博文(200)

文章存档

2009年(12)

2008年(190)

我的朋友

分类:

2008-12-16 10:17:32

12 thread control

12.3 thread attributes

Thread的一些属性可以被设置:

1Stack 的位置

2. stack的大小

3.是否是detach state

Detach statethread在结束后,系统就将其storagereclaim了,而不用别人调用pthread_join去给他收尸。所以对于不需要获取threadexit status的情况下可以使用。一个线程可以通过调用pthread_detach来将自己的状态改为detached。我们也可以将一个线程建立之前就给其加入detach属性。

4. guard size,即在一个线程的stack的结尾可以加入一段guard区域,当发生stack overflow时,访问到该区域的时候,该线程会收到signal

 

还有些附加的属性:

1Thread cancelability state

2.  thread cancelability type

3.  concurrency level

所谓的concurrency level对于采用将多个user thread分配到多个kernel threadthread s model来说是有好处的,因为提高concurrency可以增加N:M中的M,即让更多的kenel thread来完成Nthread的工作,使其更具有被调度的可能。但对于采用1: 1模型的linux来说没啥意义,因为linux里,application建立一个thread,就对应了一个kernel thread

如下是各平台的属性支持表:

Figure 12.3. POSIX.1 thread attributes

Name

Description

FreeBSD 5.2.1

Linux 2.4.22

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

 

阅读(1155) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~