Linux多线程实践(3) --线程属性

简介: 初始化/销毁线程属性int pthread_attr_init(pthread_attr_t *attr);int pthread_attr_destroy(pthread_attr_t...

初始化/销毁线程属性

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()函数返回之前就终止了,它终止以后就可能将线程号和系统资源移交给其他的线程使用

目录
相关文章
|
25天前
|
存储 监控 Java
Java多线程优化:提高线程池性能的技巧与实践
Java多线程优化:提高线程池性能的技巧与实践
49 1
|
8天前
|
Java 数据库 Android开发
一个Android App最少有几个线程?实现多线程的方式有哪些?
本文介绍了Android多线程编程的重要性及其实现方法,涵盖了基本概念、常见线程类型(如主线程、工作线程)以及多种多线程实现方式(如`Thread`、`HandlerThread`、`Executors`、Kotlin协程等)。通过合理的多线程管理,可大幅提升应用性能和用户体验。
25 15
一个Android App最少有几个线程?实现多线程的方式有哪些?
|
10天前
|
Java 数据库 Android开发
一个Android App最少有几个线程?实现多线程的方式有哪些?
本文介绍了Android应用开发中的多线程编程,涵盖基本概念、常见实现方式及最佳实践。主要内容包括主线程与工作线程的作用、多线程的多种实现方法(如 `Thread`、`HandlerThread`、`Executors` 和 Kotlin 协程),以及如何避免内存泄漏和合理使用线程池。通过有效的多线程管理,可以显著提升应用性能和用户体验。
30 10
|
17天前
|
存储 Ubuntu Linux
C语言 多线程编程(1) 初识线程和条件变量
本文档详细介绍了多线程的概念、相关命令及线程的操作方法。首先解释了线程的定义及其与进程的关系,接着对比了线程与进程的区别。随后介绍了如何在 Linux 系统中使用 `pidstat`、`top` 和 `ps` 命令查看线程信息。文档还探讨了多进程和多线程模式各自的优缺点及适用场景,并详细讲解了如何使用 POSIX 线程库创建、退出、等待和取消线程。此外,还介绍了线程分离的概念和方法,并提供了多个示例代码帮助理解。最后,深入探讨了线程间的通讯机制、互斥锁和条件变量的使用,通过具体示例展示了如何实现生产者与消费者的同步模型。
|
25天前
|
监控 安全 Java
Java多线程调试技巧:如何定位和解决线程安全问题
Java多线程调试技巧:如何定位和解决线程安全问题
71 2
|
23天前
|
机器学习/深度学习 Java TensorFlow
深度学习中的图像识别:从理论到实践Java中的多线程编程入门指南
【8月更文挑战第29天】本文将深入探讨深度学习在图像识别领域的应用,从基础理论到实际应用案例,带领读者一步步理解如何利用深度学习技术进行图像识别。我们将通过一个简单的代码示例,展示如何使用Python和TensorFlow库实现一个基本的图像识别模型。无论你是初学者还是有一定经验的开发者,都能从中获得启发和学习。 【8月更文挑战第29天】在Java世界里,线程是程序执行的最小单元,而多线程则是提高程序效率和响应性的关键武器。本文将深入浅出地引导你理解Java多线程的核心概念、创建方法以及同步机制,帮助你解锁并发编程的大门。
|
29天前
|
存储 安全 Unix
并发编程基础:使用POSIX线程(pthread)进行多线程编程。
并发编程基础:使用POSIX线程(pthread)进行多线程编程。
51 0
|
1月前
|
存储 安全 Java
【多线程面试题十七】、如果不使用synchronized和Lock,如何保证线程安全?
这篇文章探讨了在不使用`synchronized`和`Lock`的情况下保证线程安全的方法,包括使用`volatile`关键字、原子变量、线程本地存储(`ThreadLocal`)以及设计不可变对象。
【多线程面试题 一】、 创建线程有哪几种方式?
创建线程的三种方式包括继承Thread类、实现Runnable接口和实现Callable接口,其中Runnable和Callable接口方式更受推荐,因为它们允许多重继承并更好地体现面向对象思想。