线程属性有哪些,如何自定义线程属性?

线程属性有哪些,如何自定义线程属性?

C语言中文网:线程属性有哪些,如何自定义线程属性?

通过阅读前面章节,我们已经学会了如果创建一个线程,例如:

1
2
3
4
5
6
#include <pthread.h>
void * threadFun(void* args){
//......
}
pthread_t myThread;
pthread_create(&myThread, NULL, ThreadFun, NULL);

程序中,pthread_create() 函数需要传递 4 个参数,其中第二个参数 NULL 表示以系统默认的属性创建线程。

那么,线程都有哪些属性,线程的属性又该如何设置或者修改呢?接下来,我们将一一为大家解开这些疑惑。

线程属性的种类

POSIX 标准中,线程的属性用 pthread_attr_t 类型的变量表示,举个例子:

1
2
#include <pthread.h>
pthread_attr_t myAttr;

由此,我们就定义了一个表示线程属性的变量。使用此变量前,必须调用 pthread_attr_init() 函数进行初始化,该函数的语法格式如下:

1
int pthread_attr_init(pthread_attr_t * attr);

此函数定义在 <pthread.h> 头文件中,函数执行成功时,返回数字 0,反之返回非零数。

例如,对 myAttr 变量进行初始化:

1
pthread_attr_init(&myAttr);

通过调用 pthread_attr_init() 函数,myAttr 变量就拥有了系统默认的线程属性。在此基础上,我们可以根据需要对 myAttr 变量的属性值进行修改。

pthread_attr_t 是一种结构体类型,内部包含多种线程属性:

1
2
3
4
5
6
7
8
9
10
11
12
typedef struct
{
int __detachstate;
int __schedpolicy;
struct sched_param __schedparam;
int __inheritsched;
int __scope;
size_t __guardsize;
int __stackaddr_set;
void* __stackaddr;
size_t __stacksize;
} pthread_attr_t;

接下来,我们将从中挑选出几个常用的属性,给您讲解它们的功能以及修改的方法。

1) __detachstate

我们知道,默认属性的线程在执行完目标函数后,占用的私有资源并不会立即释放,要么执行完 pthread_join() 函数后释放,要么整个进程执行结束后释放。某些场景中,我们并不需要接收线程执行结束后的返回值,如果想让线程执行完后立即释放占用的私有资源,就可以通过修改 __detachstate 属性值来实现。

__detachstate 属性值用于指定线程终止执行的时机,该属性的值有两个,分别是:

  • PTHREAD_CREATE_JOINABLE(默认值):线程执行完函数后不会自行释放资源;
  • PTHREAD_CREATE_DETACHED:线程执行完函数后,会自行终止并释放占用的资源。

关于 __detachstate 属性,<pthread.h> 头文件中提供了 2 个与它相关的函数,分别是:

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

pthread_attr_getdetachstate() 函数用于获取 __detachstate 属性的值,detachstate 指针用于接收 __detachstate 属性的值;pthread_attr_setdetachstate() 函数用于修改 __detachstate 属性的值,detachstate 整形变量即为新的 __detachstate 属性值。两个函数执行成功时返回数字 0,反之返回非零数。

此外,<pthread.h> 头文件还提供有 pthread_detach() 函数,可以直接将目标线程的 __detachstate 属性改为 PTHREAD_CREATE_DETACHED,语法格式如下:

1
int pthread_detach(pthread_t thread);

函数执行成功时返回数字 0 ,反之返回非零数。

2) __schedpolicy

__schedpolicy 属性用于指定系统调度该线程所用的算法,它的值有以下 3 个:

  • SCHED_OTHER(默认值):分时调度算法;
  • SCHED_FIFO:先到先得(实时调度)算法;
  • SCHED_RR:轮转法;

其中,SCHED_OTHER 调度算法不支持为线程设置优先级,而另外两种调度算法支持。

<pthread.h> 头文件提供了如下两个函数,专门用于访问和修改 __schedpolicy 属性:

1
2
int pthread_attr_getschedpolicy(const pthread_attr_t *, int * policy)
int pthread_attr_setschedpolicy(pthread_attr_*, int policy)

pthread_attr_getschedpolicy() 函数用于获取当前 __schedpolicy 属性的值;pthread_attr_setschedpolicy() 函数用于修改 __schedpolicy 属性的值。函数执行成功时,返回值为数字 0,反之返回非零数。

3) __schedparam

__scheparam 用于设置线程的优先级(默认值为 0),该属性仅当线程的 __schedpolicy 属性为 SCHED_FIFO 或者 SCHED_RR 时才能发挥作用。

<pthread.h> 头文件中提供了如下两个函数,用于获取和修改 __schedparam 属性的值:

1
2
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);

其中,param 参数用于接收或者修改 __scheparam 属性的优先级,它是 sched_param 结构体类型的变量,定义在 <sched.h> 头文件中,内部仅有一个 sched_priority 整形变量,用于表示线程的优先级。函数执行成功时返回数字 0,反之返回非零数。

当需要修改线程的优先级时,我们只需创建一个 sched_param 类型的变量并为其内部的 sched_priority 成员赋值,然后将其传递给 pthrerd_attr_setschedparam() 函数。

不同的操作系统,线程优先级的值的范围不同,您可以通过调用如下两个系统函数获得当前系统支持的最大和最小优先级的值:

1
2
int sched_get_priority_max(int policy);   //获得最大优先级的值
int sched_get_priority_min(int policy); //获得最小优先级的值

其中,policy 的值可以为 SCHED_FIFO、SCHED_RR 或者 SCHED_OTHER,当 policy 的值为 SCHED_OTHER 时,最大和最小优先级的值都为 0。

4) __inheritsched

新建线程的调度属性(____schedpolicy 和 __schedparam 属性)默认遵循父线程的属性(谁创建它,谁就是它的父线程),如果我们想自定义线程的调度属性,就需要借助 __inheritsched 属性。

也就是说,新线程的调度属性要么遵循父线程,要么遵循 myAttr 规定的属性,默认情况下 __inheritsched 规定新线程的调度属性遵循父线程,我们也可以修改 __inheritsched 的值,使新线程的调度属性遵循自定义的属性变量(如文章开头定义的 myAttr)规定的值。

<pthread.h> 头文件提供了如下两个函数,分别用于获取和修改 __inheritsched 属性的值:

1
2
3
4
//获取 __inheritsched 属性的值
int pthread_attr_getinheritsched(const pthread_attr_t *attr,int *inheritsched);
//修改 __inheritsched 属性的值
int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched);

其中在 pthread_attr_setinheritsched() 函数中,inheritsched 参数的可选值有两个,分别是:

  • PTHREAD_INHERIT_SCHED(默认值):新线程的调度属性继承自父线程;
  • PTHREAD_EXPLICIT_SCHED:新线程的调度属性继承自 myAttr 规定的值。

以上两个函数执行成功时返回数字 0,反之返回非零数。

5) __scope

线程执行过程中,可以只和同进程内的其它线程争夺 CPU 资源,也可以和系统中所有的其它线程争夺 CPU 资源,__scope 属性用于指定目标线程和哪些线程抢夺 CPU 资源。

<pthread.h> 头文件中提供了如下两个函数,分别用于获取和修改 __scope 属性的值:

1
2
3
4
//获取 __scope 属性的值
int pthread_attr_getscope(const pthread_attr_t * attr,int * scope);
//修改 __scope 属性的值
int pthread_attr_setscope(pthread_attr_t * attr,int * scope);

当调用 pthread_attr_setscope() 函数时,scope 参数的可选值有两个,分别是:

  • PTHREAD_SCOPE_PROCESS:同一进程内争夺 CPU 资源;
  • PTHREAD_SCOPE_SYSTEM:系统所有线程之间争夺 CPU 资源。

Linux系统仅支持 PTHREAD_SCOPE_SYSTEM,即所有线程之间争夺 CPU 资源。

当函数执行成功时,返回值为数字 0,反之返回非零数。

6) __stacksize

每个线程都有属于自己的内存空间,通常称为栈(有时也称堆栈、栈空间、栈内存等)。某些场景中,线程执行可能需要较大的栈内存,此时就需要我们自定义线程拥有的栈的大小。

__stacksize 属性用于指定线程所拥有的栈内存的大小。<pthread.h> 提供有以下两个函数,分别用于获取和修改栈空间的大小:

1
2
3
4
//获取当前栈内存的大小
int pthread_attr_getstacksize(const pthread_attr_t * attr,size_t * stacksize);
//修改栈内存的大小
int pthread_attr_setsstacksize(pthread_attr_t * attr,size_t * stacksize);

函数执行成功时,返回值为数字 0,反之返回非零数。

7) __guardsize

每个线程中,栈内存的后面都紧挨着一块空闲的内存空间,我们通常称这块内存为警戒缓冲区,它的功能是:一旦我们使用的栈空间超出了额定值,警戒缓冲区可以确保线程不会因“栈溢出”立刻执行崩溃。

__guardsize 属性专门用来设置警戒缓冲区的大小,<pthread.h> 头文件中提供了如下两个函数,分别用于获取和修改 __guardsize 属性的值:

1
2
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);

pthread_attr_setguardsize() 函数中,设置警戒缓冲区的大小为参数 guardsize 指定的字节数。函数执行成功时返回数字 0,反之返回非零数。

实际应用

接下来通过一个样例,给大家演示如何自定义线程的属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
//myThread1 线程执行的函数
void *Thread1(void *arg)
{
printf("Thread1 正在执行\n");
printf("http://c.biancheng.net\n");
printf("Thread1 执行完毕\n");
return NULL;
}
//myThread2 线程执行的函数
void* Thread2(void* arg)
{
printf("Thread2 正在执行\n");
printf("C语言中文网\n");
printf("Thread2 执行完毕\n");
return NULL;
}
int main(int argc,char *argv[])
{
int num1, num2, res;
//创建两个线程
pthread_t mythread1, mythread2;
//创建两个表示线程优先级的变量
struct sched_param param1, param2;
//创建两个表示线程属性的变量
pthread_attr_t myAttr1, myAttr2;
//接收 2 个整数,用于设定线程的优先级
if (argc != 3) {
printf("未向程序传入 2 个表示优先级的数字\n");
return 0;
}
//初始化线程属性
res = pthread_attr_init(&myAttr1);
if (res != 0) {
printf("myAttr1 init Fail\n");
}

res = pthread_attr_init(&myAttr2);
if (res != 0) {
printf("myAttr1 init Fail\n");
}
//设置 myAttr1 的 __detachstate 属性值为 PTHREAD_CREATE_DETACHED
//遵循 myAttr1 属性的线程执行函数完毕后会自行释放占用私有资源,不支持 pthread_join() 函数
res = pthread_attr_setdetachstate(&myAttr1, PTHREAD_CREATE_DETACHED);
if (res != 0) {
printf("myAttr1 set_detachstate Fail\n");
}
//设置 myAttr1 的 __scope 属性值为 PTHREAD_SCOPE_SYSTEM
//遵循 myAttr1 属性的线程将同系统中的所有其它线程争夺 CPU 资源
res = pthread_attr_setscope(&myAttr1, PTHREAD_SCOPE_SYSTEM);
if (res != 0) {
printf("myAttr1 set_scope Fail\n");
}
//设置 myAttr2 的 __scope 属性值为 PTHREAD_SCOPE_SYSTEM
//遵循 myAttr2 属性的线程将同系统中的所有其它线程争夺 CPU 资源
res = pthread_attr_setscope(&myAttr2, PTHREAD_SCOPE_SYSTEM);
if (res != 0) {
printf("myAttr2 set_scope Fail\n");
}
//设置 myAttr1 的 __schedpolicy 属性值为 SCHED_FIFO
//系统会以实时调用的方式执行遵循 myAttr1 属性的线程
res = pthread_attr_setschedpolicy(&myAttr1, SCHED_FIFO);
if (res != 0) {
printf("myAttr1 set_policy Fail\n");
}

//设置 myAttr2 的 __schedpolicy 属性值为 SCHED_FIFO
//系统会以实时调用的方式执行遵循 myAttr2 属性的线程
res = pthread_attr_setschedpolicy(&myAttr2, SCHED_FIFO);
if (res != 0) {
printf("myAttr2 set_policy Fail\n");
}
//设置 myAttr1 的 __inheritsched 属性值为 PTHREAD_EXPLICIT_SCHED
//myAttr1 属性的线程将遵循自定义的线程属性
res = pthread_attr_setinheritsched(&myAttr1, PTHREAD_EXPLICIT_SCHED);
if (res != 0) {
printf("myAttr1 set_inheritsched fail\n");
}

//设置 myAttr2 的 __inheritsched 属性值为 PTHREAD_EXPLICIT_SCHED
//myAttr2 属性的线程将遵循自定义的线程属性
res = pthread_attr_setinheritsched(&myAttr2, PTHREAD_EXPLICIT_SCHED);
if (res != 0) {
printf("myAttr2 set_inheritsched fail\n");
}
//想 argv[] 数组中的字符转换为数字
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);
// 分别将 num1 和 num2 作为线程优先级的值
param1.sched_priority = num1;
param2.sched_priority = num2;
//设置 myAttr1 属性的优先级为 param1
res = pthread_attr_setschedparam(&myAttr1, &param1);
if (res != 0) {
printf("param1 setscheparam Fail\n");
}
//设置 myAttr2 属性的优先级为 param2
res = pthread_attr_setschedparam(&myAttr2, &param2);
if (res != 0) {
printf("param2 setscheparam Fail\n");
}
//创建新线程并遵循 myAttr1 属性
res = pthread_create(&mythread1, &myAttr1, Thread1, NULL);
if (res != 0) {
printf("mythread1 create Fail\n");
}
//创建新线程并遵循 myAttr2 属性
res = pthread_create(&mythread2, &myAttr2, Thread2, NULL);
if (res != 0) {
printf("mythread2 create Fail\n");
}
sleep(5); //等待 mythread1 和 mythread2 两个线程执行完
//尝试 pthread_join() 函数等待 mythread1 线程执行结束
res = pthread_join(mythread1, NULL);
if (res != 0) {
if (res == EINVAL) {
printf("mythread1不支持调用 pthread_join()函数\n");
}
}
//尝试等待 mythread2 线程执行结束
res = pthread_join(mythread2, NULL);
if (res != 0) {
printf("mythread2 has finished\n");
}
printf("主线程执行完毕\n");
return 0;
}

假设程序编写在 thread.c 文件中,执行过程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost ~]# gcc thread.c -o thread.exe -lpthread
[root@localhost ~]# ./thread.exe 30 3
Thread1 正在执行
http://c.biancheng.net
Thread1 执行完毕
Thread2 正在执行
C语言中文网
Thread2 执行完毕
mythread1不支持调用 pthread_join()函数
主线程执行完毕
[root@localhost ~]# ./thread.exe 3 30
Thread2 正在执行
C语言中文网
Thread2 执行完毕
Thread1 正在执行
http://c.biancheng.net
Thread1 执行完毕
mythread1不支持调用 pthread_join()函数
主线程执行完毕

上面展示了两组执行结果,分别为 mythread1 和 mythread2 设置了不同的优先级,从运行结果可以看到,哪个线程的优先级高(数值大),哪个线程先执行。

此外,通过程序的执行结果还可以看出,由于 mythread 线程的 __detachstate 属性为 PTHREAD_CREATE_DETACHED,因此该线程执行完 Thread1() 函数后会自行终止并释放占用的私有资源,不需要也不允许在其它线程(比如主线程)中执行 pthread_join(mythread1, NULL) 函数。