初始化/销毁线程属性

int pthread_attr_init(pthread_attr_t *attr);
int pthread_attr_destroy(pthread_attr_t *attr);

线程分离属性

int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate);
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);

参数说明:

The following values may be specified in detachstate:

PTHREAD_CREATE_DETACHED

Threads that are created using attr will be created in a detached state.

PTHREAD_CREATE_JOINABLE

Threads that are created using attr will be created in a joinable state.

The  default  setting  of  the  detach  state  attribute  in  a  newly initialized

threadattributes object is PTHREAD_CREATE_JOINABLE.

The pthread_attr_getdetachstate() returns  the  detach  state  attribute  of  the

threadattributes object attr in the buffer pointed to by detachstate.

线程栈大小

int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);

DESCRIPTION

The pthread_attr_setstacksize() function sets the stack  size  attribute

of  the  threadattributes object referred to by attr to the value specified

in stacksize.(一般情况下该值我们设置为0,使用系统默认设置的线程栈大小,否则可能会引起程序的可移植性的问题)       The  stack  size  attribute determines the minimum size (in bytes) that will

be allocatedfor threads created using the thread attributes object attr.

The pthread_attr_getstacksize() function returns the stack size attribute of

the  threadattributes object referred to by attr in the buffer pointed to by stacksize.

线程栈溢出保护区大小

int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
int pthread_attr_getguardsize(pthread_attr_t *attr, size_t *guardsize);

线程竞争范围(进程范围内的竞争 or 系统范围内的竞争)

int pthread_attr_getscope(const pthread_attr_t *attr,int *contentionscope);
int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);

contentionscope说明:

PTHREAD_SCOPE_SYSTEM

The  thread  competes for resources with all other threads in all processes on

the system that are in the same scheduling allocation domain (a group of one  or  more processors).

PTHREAD_SCOPE_SYSTEM  threads are scheduled relative to one anotheraccording to their

scheduling policy and priority.

PTHREAD_SCOPE_PROCESS

The thread competes for resources with all other threads in the same process  thatwere

also created with the PTHREAD_SCOPE_PROCESS contention scope.

PTHREAD_SCOPE_PROCESS threads are scheduled  relative  to  other  threads  in  the process

according to their scheduling policy and priority.  POSIX.1-2001 leaves it unspecified how these threads contend with other threads in other process

on  the system  or  with  other  threads  in  the  same process that were created with the

PTHREAD_SCOPE_SYSTEM contention scope.

线程调度策略

int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy);
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);

DESCRIPTION

The pthread_attr_setschedpolicy() function sets the scheduling policy  attribute  of  the thread

attributes  object  referred  to  by attr to the value specified in policy.  This attribute determines

the  scheduling  policy  of  a  thread  created  using  the  thread attributes object attr.

The  supported  values  for  policy  are  SCHED_FIFO, SCHED_RR, and SCHED_OTHER, as below:

SCHED_FIFO    a first-in, first-out policy(先进先出调度策略);

SCHED_RR      a round-robin policy(时间片轮转调度算法);

SCHED_OTHER   the standard round-robin time-sharing policy(线程一旦开始运行,直到被抢占或者直到线程阻塞或停止为止);

注意:

In order for the policy setting made by pthread_attr_setschedpolicy()

to have effect when calling pthread_create(3), the caller must use pthread_attr_setinheritsched(3)(见下)

to set the inherit-scheduler attribute of the attributes object attr to PTHREAD_EXPLICIT_SCHED(新创建的进程继承自己的调度属性).

线程继承的调度策略

int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched);
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched);

The following values may be specified in inheritsched:

PTHREAD_INHERIT_SCHED(继承调度属性)

Threads that are created using attr inherit scheduling attributes from the

creating thread; the scheduling attributes in attr are ignored.

PTHREAD_EXPLICIT_SCHED(指定自己的调度属性)

Threads that are created using attr take their scheduling attributes from

the values specified by the attributes object.

The default setting of the inherit-scheduler attribute  in  a  newly  initialized  thread attributes object is PTHREAD_INHERIT_SCHED.

线程调度参数(实际上我们一般只关心一个参数:线程的优先级,默认为0)

int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param);
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param);
//sched_param结构体
struct sched_param {
    int sched_priority;     /* Scheduling priority */
};

线程的并发级别

int pthread_setconcurrency(int new_level);
int pthread_getconcurrency(void);

说明:并发级别仅在N:M线程模型中有效,设置并发级别,给内核一个提示:表示提供给定级别数量的核心线程来映射用户线程是高效的(仅仅是一个提示),默认为0, 内核按照默认的方式进行并发;

/** 查看线程默认属性 **/
void printThreadAttr()
{
    pthread_attr_t attr;
    pthread_attr_init(&attr);

    int detachstate;
    pthread_attr_getdetachstate(&attr, &detachstate);
    cout << "detach-state: "
         << (detachstate == PTHREAD_CREATE_JOINABLE ?
             "PTHREAD_CREATE_JOINABLE" : "PTHREAD_CREATE_DETACHED")
         << endl;

    size_t size;
    pthread_attr_getstacksize(&attr, &size);
    cout << "stack-size: " << size << endl;

    pthread_attr_getguardsize(&attr, &size);
    cout << "guard-size: " << size << endl;

    int scope;
    pthread_attr_getscope(&attr, &scope);
    cout << "scope: "
         << (scope == PTHREAD_SCOPE_SYSTEM ?
             "PTHREAD_SCOPE_SYSTEM" : "PTHREAD_SCOPE_PROCESS")
         << endl;

    int policy;
    pthread_attr_getschedpolicy(&attr, &policy);
    cout << "policy: ";
    switch (policy)
    {
    case SCHED_FIFO:
        cout << "SCHED_FIFO";
        break;
    case SCHED_RR:
        cout << "SCHED_RR";
        break;
    case SCHED_OTHER:
        cout << "SCHED_OTHER";
        break;
    default:
        break;
    }
    cout << endl;

    int inheritsched;
    pthread_attr_getinheritsched(&attr, &inheritsched);
    cout << "inheritsched: "
         << (inheritsched == PTHREAD_INHERIT_SCHED ?
             "PTHREAD_INHERIT_SCHED" : "PTHREAD_INHERIT_SCHED")
         << endl;

    struct sched_param param;
    pthread_attr_getschedparam(&attr, ¶m);
    cout << "scheduling priority: " << param.sched_priority << endl;
    cout << "concurrency: " << pthread_getconcurrency() << endl;
    pthread_attr_destroy(&attr);
}



说明:

绑定属性:

Linux中采用“一对一”的线程机制,也就是一个用户线程对应一个内核线程。绑定属性就是指一个用户线程固定地分配给一个内核线程,因为CPU时间片的调度是面向内核线程(也就是轻量级进程)的,因此具有绑定属性的线程可以保证在需要的时候总有一个内核线程与之对应。而与之对应的非绑定属性就是指用户线程和内核线程的关系不是始终固定的,而是由系统来控制分配的。

分离属性:

分离属性是用来决定一个线程以什么样的方式来终止自己。在非分离情况下,当一个线程结束时,它所占用的系统资源并没有被释放,也就是没有真正的终止。只有当pthread_join()函数返回时,创建的线程才能释放自己占用的系统资源。而在分离属性情况下,一个线程结束时立即释放它所占有的系统资源。这里要注意的一点是,如果设置一个线程的分离属性,而这个线程运行又非常快,那么它很可能在pthread_create()函数返回之前就终止了,它终止以后就可能将线程号和系统资源移交给其他的线程使用。

Linux多线程实践(3) --线程属性的更多相关文章

  1. Linux多线程实践(2) --线程基本API

    POSIX线程库 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以"pthread_"开头,要使用这些函数库,要通过引入头文<pthread.h>,而且链 ...

  2. Linux多线程实践(1) --线程理论

    线程概念 在一个程序里的一个执行路线就叫做线程(thread).更准确的定义是:线程是"一个进程内部的控制序列/指令序列"; 一切进程至少有一个执行线程; 进程  VS. 线程  ...

  3. Linux多线程实践(4) --线程特定数据

    线程特定数据 int pthread_key_create(pthread_key_t *key, void (*destr_function) (void *)); int pthread_key_ ...

  4. Linux多线程实践(三)线程的基本属性设置API

    POSIX 线程库定义了线程属性对象 pthread_attr_t ,它封装了线程的创建者能够訪问和改动的线程属性.主要包含例如以下属性: 1. 作用域(scope) 2. 栈尺寸(stack siz ...

  5. Linux学习笔记22——线程属性(转)

    本文来自博客园:http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764204.html 一.线程属性线程具有属性,用pthread_at ...

  6. Linux c编程:线程属性

    前面介绍了pthread_create函数,并且当时的例子中,传入的参数都是空指针,而不是指向pthread_attr_t结构的指针.可以使用pthread_attr_t结构修改线程默认属性,并把这些 ...

  7. Linux多线程实践(9) --简单线程池的设计与实现

    线程池的技术背景 在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源.在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收.所以 ...

  8. Linux多线程实践(一)线程基本概念和理论

    线程概念 在一个程序里的一个运行路线就叫做线程(thread).更准确的定义是:线程是"一个进程内部的控制序列/指令序列"; 对于每一个进程至少有一个运行线程; 进程  VS. 线 ...

  9. Linux多线程实践(四 )线程的特定数据

    在单线程程序中.我们常常要用到"全局变量"以实现多个函数间共享数据, 然而在多线程环境下.因为数据空间是共享的.因此全局变量也为全部线程所共同拥有.但有时应用程序设计中有必要提供线 ...

随机推荐

  1. flask+apscheduler+redis实现定时任务持久化

    在我们开发flask的时候,我们会结合apscheduler实现定时任务,我们部署到服务器上,会不会遇到这样的问题,每次我们部署后,我们重启服务后,原来的定时任务都需要重启,这样对我们经常迭代的项目肯 ...

  2. React-报错Warning:setState(...)on anunmounted component

    一.原因        这种错误一般出现在react组件已经从DOM中移除.我们在react组件中发送一些异步请求的时候,就可能会出现这样的问题.举个例子,我们在componentWillMount中 ...

  3. 排序算法的C语言实现(上 比较类排序:插入排序、快速排序与归并排序)

    总述:排序是指将元素集合按规定的顺序排列.通常有两种排序方法:升序排列和降序排列.例如,如整数集{6,8,9,5}进行升序排列,结果为{5,6,8,9},对其进行降序排列结果为{9,8,6,5}.虽然 ...

  4. MongoDB 固定集合

    MongoDB 固定集合(Capped Collections)是性能出色且有着固定大小的集合,对于大小固定,我们可以想象其就像一个环形队列,当集合空间用完后,再插入的元素就会覆盖最初始的头部的元素! ...

  5. Java中的Lock锁

    Lock锁介绍: 在java中可以使用 synchronized 来实现多线程下对象的同步访问,为了获得更加灵活使用场景.高效的性能,java还提供了Lock接口及其实现类ReentrantLock和 ...

  6. OpenResty 执行阶段的概念和用途

    主要还是 Nginx 的执行阶段知识了,都是因为 OR 才会那么深刻, 它有些自己的阶段. 主要还是参照 春哥的 Nginx 教程 请多读几遍,如果不清楚nginx的执行阶段就无法充分利用 openr ...

  7. iOS开源加密相册Agony的实现(二)

    简介 虽然目前市面上有一些不错的加密相册App,但不是内置广告,就是对上传的张数有所限制.本文介绍了一个加密相册的制作过程,该加密相册将包括多密码(输入不同的密码即可访问不同的空间,可掩人耳目).Wi ...

  8. 剑指Offer——网易笔试题+知识点总结

    剑指Offer--网易笔试题+知识点总结 Fibonacci package cn.edu.ujn.nk; import java.util.ArrayList; import java.util.S ...

  9. Scheme call/cc 研究

    目前尚不清楚实质,但已经能够从形式上理解它的某些好处,有个很简单的连乘函数可以说明: 为了展示究竟发生了什么,我包装了下乘法函数,将其变为mul. 我们将比较product和xproduct的区别. ...

  10. SQLite Where 子句(http://www.w3cschool.cc/sqlite/sqlite-where-clause.html)

    SQLite Where 子句 SQLite的 WHERE 子句用于指定从一个表或多个表中获取数据的条件. 如果满足给定的条件,即为真(true)时,则从表中返回特定的值.您可以使用 WHERE 子句 ...