linux多线程学习(三)——线程属性设置。

简介:

在上一篇文章中,介绍了线程的创建和退出,以及相关函数的使用。其中pthread_create函数的第二个参数,是关于线程属性的设置,这也是今天所有讲述的。这些属性主要包括邦定属性、分离属性、堆栈地址、堆栈大小、优先级。其中系统默认的是非邦定、非分离、缺省1M的堆栈、与父进程同样级别的优先级。在pthread_create中,把第二个参数设置为NULL的话,将采用默认的属性配置。

(1)绑定属性。

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

(2)分离属性。

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

这些属性都是通过一些函数来完成的,通常先调用pthread_attr_init来初始化,之后来调用相应的属性设置函数。

1、pthread_attr_init

功能:        对线程属性变量的初始化。

头文件:     <pthread.h>

函数原型:   int pthread_attr_init (pthread_attr_t* attr);

函数传入值:attr:线程属性。

函数返回值:成功: 0

                失败: -1

2、pthread_attr_setscope

功能:        设置线程绑定属性。

头文件:     <pthread.h>

函数原型:   int pthread_attr_setscope (pthread_attr_t* attr, int scope);

函数传入值:attr: 线程属性。

                scope:PTHREAD_SCOPE_SYSTEM(绑定)

                         PTHREAD_SCOPE_PROCESS(非绑定)

函数返回值得:同1。

3、pthread_attr_setdetachstate

功能:        设置线程分离属性。

头文件:      <phread.h>

函数原型:    int pthread_attr_setdetachstate (pthread_attr_t* attr, int detachstate);

函数传入值:attr:线程属性。

                detachstate:PTHREAD_CREATE_DETACHED(分离)

                                  PTHREAD_CREATE_JOINABLE(非分离)

函数返回值得:同1。

4、pthread_attr_getschedparam

功能:       得到线程优先级。

头文件:    <pthread.h>

函数原型:  int pthread_attr_getschedparam (pthread_attr_t* attr, struct sched_param* param);

函数传入值:attr:线程属性;

                param:线程优先级;

函数返回值:同1。

5、pthread_attr_setschedparam

功能:       设置线程优先级。

头文件:     <pthread.h>

函数原型:   int pthread_attr_setschedparam (pthread_attr_t* attr, struct sched_param* param);

函数传入值:attr:线程属性。

                 param:线程优先级。

函数返回值:同1。

 

函数实现:

 

[cpp:firstline[0]] view plaincopy
  1. #include <stdlib.h>    
  2. #include <stdio.h>    
  3. #include <errno.h>    
  4. #include <pthread.h>    
  5.     
  6. static void* pthread_func_1 (void*);    
  7. static void* pthread_func_2 (void*);    
  8.     
  9. int main (int argc, char** argv)    
  10. {    
  11.   pthread_t pt_1 = 0;    
  12.   pthread_t pt_2 = 0;    
  13.   pthread_attr_t attr = {0};    
  14.   int ret = 0;    
  15.     
  16.   pthread_attr_init (&attr); //属性设置  
  17.   pthread_attr_setscope (&attr, PTHREAD_SCOPE_SYSTEM);    
  18.   pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);    
  19.       
  20.   ret = pthread_create (&pt_1, &attr, pthread_func_1, NULL);    
  21.   if (ret != 0)    
  22.   {    
  23.     perror ("pthread_1_create");    
  24.   }    
  25.       
  26.   ret = pthread_create (&pt_2, NULL, pthread_func_2, NULL);    
  27.   if (ret != 0)    
  28.   {    
  29.     perror ("pthread_2_create");    
  30.   }    
  31.     
  32.   pthread_join (pt_2, NULL);    
  33.     
  34.   return 0;    
  35. }    
  36.     
  37. static void* pthread_func_1 (void* data)    
  38. {    
  39.   int i = 0;    
  40.       
  41.   for (; i < 6; i++)    
  42.   {     
  43.     printf ("This is pthread_1./n");    
  44.        
  45.     if (i == 2)    
  46.     {    
  47.       pthread_exit (0);    
  48.     }    
  49.   }    
  50.     
  51.   return;    
  52. }    
  53.     
  54. static void* pthread_func_2 (void* data)    
  55. {    
  56.   int i = 0;    
  57.     
  58.   for (; i < 3; i ++)    
  59.   {    
  60.     printf ("This is pthread_2./n");    
  61.   }    
  62.     
  63.   return;    
  64. }    

从上面事例中,可以得到这么一个结果,就是线程一的线程函数一结束就自动释放资源,线程二就得等到pthread_join来释放系统资源。在下一篇文章中将介绍线程锁。

 

~~END~~

相关文章
|
23天前
|
Linux C++
LInux下Posix的传统线程示例
LInux下Posix的传统线程示例
19 1
|
3天前
|
Unix Linux Windows
Linux的学习之路:3、基础指令(2)
Linux的学习之路:3、基础指令(2)
26 0
|
2天前
|
固态存储 Ubuntu Linux
Linux(29) 多线程快速解压缩|删除|监视大型文件
Linux(29) 多线程快速解压缩|删除|监视大型文件
11 1
|
3天前
|
Ubuntu Linux
Linux(22) Linux设置网络优先级顺序
Linux(22) Linux设置网络优先级顺序
3 0
|
3天前
|
消息中间件 Unix Linux
Linux的学习之路:17、进程间通信(1)
Linux的学习之路:17、进程间通信(1)
18 1
|
3天前
|
存储 安全 Linux
Linux的学习之路:9、冯诺依曼与进程(1)
Linux的学习之路:9、冯诺依曼与进程(1)
18 0
|
14天前
|
Linux 数据安全/隐私保护
Linux设置PPPOE
请注意,以上步骤是基本的设置流程。具体步骤可能会因Linux发行版和版本的不同而有所差异,确保按照你所使用的系统来进行设置。如果使用图形界面,也可以在网络设置中配置PPPoE连接。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
22 0
|
16天前
|
安全 Unix Linux
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
17 0
|
18天前
|
Linux 应用服务中间件 网络安全
小白学习Linux的学习建议和阶段
【4月更文挑战第5天】小白学习Linux的学习建议和阶段
46 0
|
29天前
|
Linux API C++
【C++ 线程包裹类设计】跨平台C++线程包装类:属性设置与平台差异的全面探讨
【C++ 线程包裹类设计】跨平台C++线程包装类:属性设置与平台差异的全面探讨
51 2