Thread Properties
Property values cannot be set directly and must be manipulated using related functions
The initialization function is pthread_attr_init, and the function must be called before the Pthread_create function
typedefstruct{ int detachstate; // 线程的分离状态 int scope; // 线程绑定状态 int // 线程调度策略 struct// 线程的调度参数 int inheritsched; //线程的继承性 size_t // 线程栈末尾的警戒缓冲区大小 void * stackaddr; // 线程栈的位置 size_t stacksize; // 线程栈的大小}pthread_attr_t;
To delete a thread Property object
函数原型int*attr); 返回值成功返回0
Thread Detach State (Detachstate)
*attrint detachstate)detachstate选值PTHREAD_CREATE_DETACHED(分离线程) PTHREAD _CREATE_JOINABLE(非分离线程)int*attrint*detachstate
Thread Binding Scope
Lightweight process
Can be understood as kernel threads, located between the user layer and the system layer
The system realizes the allocation of thread resources and the control of threads through lightweight processes.
A lightweight process can control one or more threads
Non-binding
How many lightweight processes are started, and which lightweight processes control which threads are controlled by the system
Default handling of the system
Binding
A thread pinned "tied" on top of a lightweight process
The bound thread has a high response speed because the CPU time slice is scheduled to be light-process oriented, and the bound thread can guarantee that it always has a light process available when needed
By setting the priority and scheduling level of the bound light process, the bound thread can satisfy requirements such as real-time response
Thread Binding property setting function
intscope)参数说明attr:指向属性结构的指针scope:绑定类型PTHREAD_SCOPE_SYSTEM(绑定的)PTHREAD_SCOPE_PROCESS(非绑定的)举例/*初始化属性值,均设为默认值*/pthread_attr_init(&attr); pthread_attr_setscope(&attr, THREAD_SCOPE_SYSTEM);pthread_create(&tid, &attr, (void *) my_function, NULL);
Thread Scheduling Policy Schedpolicy
函数原型int*attrint*policy);int*attrint policy);参数说明attr:线程属性变量policy:调度策略SCHED_FIFO:先进先出SCHED_RR:轮转法SCHED_OTHER:其它返回值成功返回0失败返回-1
Thread scheduling parameter Schedparam
函数原型int pthread_attr_getschedparam(conststruct sched_param *param);intconststruct sched_param *param);参数说明attr:线程属性param:sched_param结构sched_priority:控制一个优先权值sched_get_priority_min():获取系统支持的最小优先权值sched_get_priority_max ():获取系统支持的最大优先权值
The inheritance of Threads inheritsched
设置继承调度策略PTHREAD_INHERIT_SCHED(缺省值) 创建的线程将具有父线程的调度策略属性对象中的调度策略参数将不起作用PTHREAD_EXPLICIT_SCHED线程的调度策略和父线程无关属性对象中的调度策略参数将起作用相关设置函数int*tattrintint*tattrint*inherit
Stack Protection Zone Guardsize
缺省情况下,线程的堆栈保护区大小因系统版本不同而异取值说明若guardsize=0,创建线程时将不会创建堆栈保护区若guardsize>0,创建线程时将至少有一个guardsize大小的堆栈保护区使用说明创建线程堆栈时,一般将guardsize参数值上舍入成系统变量PAGESIZE的整数倍,来分配堆栈保护区但程序调用pthread_attr_getguardsize()函数获得的堆栈保护区大小将是调用pthread_attr_setguardsize()函数时指定的guardsize参数值相关函数int*attrint*attr*guardsize
Base address of the thread stack stackaddr
默认由系统分配如果stackaddr为非空值,线程堆栈将从指定地址开始如果是NULL (缺省值) ,pthread_create()为其分配堆栈 相关函数int*tattr*stackaddrint*tattr**
Thread stack size stacksize
定义系统为线程分配的堆栈大小(字节)默认由系统决定大小函数原型int*tattrintint*tattr*size); 参数说明size:线程所需堆栈的大小size=0,系统使用缺省大小在大多数情况下,缺省值是最合适的堆栈的大小不能小于系统定义的最小堆栈容量PTHREAD_STACK_MIN
One-time initialization of threads
Sometimes you need to initialize only one time for some POSIX variables (such as line Cheng), and if you initialize multiple times, the program will get an error
In sequential programming, Boolean variables are often used to initialize at once
The control variable is statically initialized to 0, and any code that relies on initialization can test the variable
If the value of the variable is still 0, it can initialize, then set the variable to 1, and the code to be checked will skip initialization
But in multi-threaded programming, things get very complicated.
If multiple threads execute initialization sequence code concurrently, two threads may find the control variable to be 0, and both are initialized, and the process should be executed only once
The state of the initialization must be protected by a mutex
Initialization of a POSIX static variable can be controlled using a mutex to initialize the variable
However, dynamic initialization of variables using Pthread_once can be a lot easier
One-time initialization function for threads
Function prototypes
pthread_once_t Once_control=pthread_once_init;
int pthread_once (pthread_once_t *once_control, Void (*init_routine) (void));
Parameters
Once_control: The control variable must be statically initialized with the PTHREAD_ONCE_INIT macro
Init_routine: Initialization function
Description
The Pthread_once function first checks the control variable to determine if the initialization has been completed
If it is complete, return
Otherwise, the initialization function is called, and the record initialization is completed
If another thread calls Pthread_once at the beginning of a thread, the calling thread waits until the thread finishes initializing and returns
Private data for a thread
Background
All threads within a process share the same address space, any variable declared as static or external, or a variable declared on the process heap, can be read and written by all threads of the process
How can I make a wire program own private data?
Function prototypes
int pthread_key_create (Pthread_key key, Void (*destructor) (void ));
Parameters
Key: Private Data key
Destructor: Cleanup function
If the argument is not empty, the system calls this function to release the block of memory bound to the key when each thread ends.
Description
The function assigns an item from the thread private data pool, assigns its value to the key for later access use
Regardless of which thread calls Pthread_key_create (), the key created is accessible to all threads, but each thread can fill a key with different values according to its own needs, which is equivalent to providing a global variable with the same name and different values
This function is often used in conjunction with the function pthread_once so that the key is created only once
函数原型intconstvoidvoid * pthread_getspecific(pthread_key_t key) 说明pthread_setspecific()将pointer的值(不是所指的内容)与key相关联pthread_getspecific()将与key相关联的数据读出来数据类型都设为void *,因此可以指向任何类型的数据
Description
Create two threads individually set the same thread private data for their thread ID
To verify its privacy, the program staggered the write and read time of two threads of private data
As can be seen from the program running results, two threads do not interfere with the change of TSD
The cleanup function executes automatically when the thread exits, and the parameter is TID
Linux-Threading Attribute control