- 浏览: 1331955 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
chinaxy1:
...
ON DUPLICATE KEY UPDATE重复插入时更新 -
b_l_east:
不一定哦,就算大小相当,in 和 exists的性能也会很大差 ...
mysql查询语句in和exists二者的区别和性能影响 -
llp1990311:
[size=x-small][/size]
如何正确防御xss攻击 -
home198979:
q315506754 写道还是佩服写c的 用其它语言一样可以实 ...
玩转深度优先搜索算法 -
q315506754:
还是佩服写c的
玩转深度优先搜索算法
一.线程属性
线程具有属性,用pthread_attr_t表示,在对该结构进行处理之前必须进行初始化,在使用后需要对其去除初始化。我们用pthread_attr_init函数对其初始化,用pthread_attr_destroy对其去除初始化。
名称:: |
pthread_attr_init/pthread_attr_destroy |
功能: |
对线程属性初始化/去除初始化 |
头文件: |
#include <pthread.h> |
函数原形: |
int pthread_attr_init(pthread_attr_t *attr); int pthread_attr_destroy(pthread_attr_t *attr); |
参数: |
Attr 线程属性变量 |
返回值: |
若成功返回0,若失败返回-1。 |
调用pthread_attr_init之后,pthread_t结构所包含的内容就是操作系统实现支持的线程所有属性的默认值。
如果要去除对pthread_attr_t结构的初始化,可以调用pthread_attr_destroy函数。如果pthread_attr_init实现时为属性对象分配了动态内存空间,pthread_attr_destroy还会用无效的值初始化属性对象,因此如果经pthread_attr_destroy去除初始化之后的pthread_attr_t结构被pthread_create函数调用,将会导致其返回错误。
线程属性结构如下:
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;
每个个属性都对应一些函数对其查看或修改。下面我们分别介绍。
二、线程的分离状态
线程的分离状态决定一个线程以什么样的方式来终止自己。在默认情况下线程是非分离状态的,这种情况下,原有的线程等待创建的线程结束。只有当pthread_join()函数返回时,创建的线程才算终止,才能释放自己占用的系统资源。
而分离线程不是这样子的,它没有被其他的线程所等待,自己运行结束了,线程也就终止了,马上释放系统资源。程序员应该根据自己的需要,选择适当的分离状态。所以如果我们在创建线程时就知道不需要了解线程的终止状态,则可以pthread_attr_t结构中的detachstate线程属性,让线程以分离状态启动。
名称:: |
pthread_attr_getdetachstate/pthread_attr_setdetachstate |
功能: |
获取/修改线程的分离状态属性 |
头文件: |
#include <pthread.h> |
函数原形: |
int pthread_attr_getdetachstate(const pthread_attr_t * attr,int *detachstate); int pthread_attr_setdetachstate(pthread_attr_t *attr,int detachstate); |
参数: |
Attr 线程属性变量 Detachstate 线程的分离状态属性 |
返回值: |
若成功返回0,若失败返回-1。 |
可以使用pthread_attr_setdetachstate函数把线程属性detachstate设置为下面的两个合法值之一:设置为PTHREAD_CREATE_DETACHED,以分离状态启动线程;或者设置为PTHREAD_CREATE_JOINABLE,正常启动线程。可以使用pthread_attr_getdetachstate函数获取当前的datachstate线程属性。
以分离状态创建线程
#iinclude <pthread.h>
void *child_thread(void *arg) { printf(“child thread run!\n”); }
int main(int argc,char *argv[ ]) { pthread_t tid; pthread_attr_t attr;
pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED); pthread_create(&tid,&attr,fn,arg); pthread_attr_destroy(&attr); sleep(1); } |
三、线程的继承性
函数pthread_attr_setinheritsched和pthread_attr_getinheritsched分别用来设置和得到线程的继承性,这两个函数的定义如下:
名称:: |
pthread_attr_getinheritsched pthread_attr_setinheritsched |
功能: |
获得/设置线程的继承性 |
头文件: |
#include <pthread.h> |
函数原形: |
int pthread_attr_getinheritsched(const pthread_attr_t *attr,int *inheritsched); int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched); |
参数: |
attr 线程属性变量 inheritsched 线程的继承性 |
返回值: |
若成功返回0,若失败返回-1。 |
这两个函数具有两个参数,第1个是指向属性对象的指针,第2个是继承性或指向继承性的指针。继承性决定调度的参数是从创建的进程中继承还是使用在schedpolicy和schedparam属性中显式设置的调度信息。Pthreads不为inheritsched指定默认值,因此如果你关心线程的调度策略和参数,必须先设置该属性。
继承性的可能值是PTHREAD_INHERIT_SCHED(表示新现成将继承创建线程的调度策略和参数)和PTHREAD_EXPLICIT_SCHED(表示使用在schedpolicy和schedparam属性中显式设置的调度策略和参数)。
如果你需要显式的设置一个线程的调度策略或参数,那么你必须在设置之前将inheritsched属性设置为PTHREAD_EXPLICIT_SCHED.
下面我来讲进程的调度策略和调度参数。我会结合下面的函数给出本函数的程序例子。
四、线程的调度策略
函数pthread_attr_setschedpolicy和pthread_attr_getschedpolicy分别用来设置和得到线程的调度策略。
名称:: |
pthread_attr_getschedpolicy pthread_attr_setschedpolicy |
功能: |
获得/设置线程的调度策略 |
头文件: |
#include <pthread.h> |
函数原形: |
int pthread_attr_getschedpolicy(const pthread_attr_t *attr,int *policy); int pthread_attr_setschedpolicy(pthread_attr_t *attr,int policy); |
参数: |
attr 线程属性变量 policy 调度策略 |
返回值: |
若成功返回0,若失败返回-1。 |
这两个函数具有两个参数,第1个参数是指向属性对象的指针,第2个参数是调度策略或指向调度策略的指针。调度策略可能的值是先进先出(SCHED_FIFO)、轮转法(SCHED_RR),或其它(SCHED_OTHER)。
SCHED_FIFO策略允许一个线程运行直到有更高优先级的线程准备好,或者直到它自愿阻塞自己。在SCHED_FIFO调度策略下,当有一个线程准备好时,除非有平等或更高优先级的线程已经在运行,否则它会很快开始执行。
SCHED_RR(轮循)策略是基本相同的,不同之处在于:如果有一个SCHED_RR
策略的线程执行了超过一个固定的时期(时间片间隔)没有阻塞,而另外的SCHED_RR或SCHBD_FIPO策略的相同优先级的线程准备好时,运行的线程将被抢占以便准备好的线程可以执行。
当有SCHED_FIFO或SCHED_RR策赂的线程在一个条件变量上等持或等持加锁同一个互斥量时,它们将以优先级顺序被唤醒。即,如果一个低优先级的SCHED_FIFO线程和一个高优先织的SCHED_FIFO线程都在等待锁相同的互斥且,则当互斥量被解锁时,高优先级线程将总是被首先解除阻塞。
五、线程的调度参数
函数pthread_attr_getschedparam 和pthread_attr_setschedparam分别用来设置和得到线程的调度参数。
名称:: |
pthread_attr_getschedparam pthread_attr_setschedparam |
功能: |
获得/设置线程的调度参数 |
头文件: |
#include <pthread.h> |
函数原形: |
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); |
参数: |
attr 线程属性变量 param sched_param结构 |
返回值: |
若成功返回0,若失败返回-1。 |
这两个函数具有两个参数,第1个参数是指向属性对象的指针,第2个参数是sched_param结构或指向该结构的指针。结构sched_param在文件/usr/include /bits/sched.h中定义如下:
struct sched_param
{
int sched_priority;
};
结构sched_param的子成员sched_priority控制一个优先权值,大的优先权值对应高的优先权。系统支持的最大和最小优先权值可以用sched_get_priority_max函数和sched_get_priority_min函数分别得到。
注意:如果不是编写实时程序,不建议修改线程的优先级。因为,调度策略是一件非常复杂的事情,如果不正确使用会导致程序错误,从而导致死锁等问题。如:在多线程应用程序中为线程设置不同的优先级别,有可能因为共享资源而导致优先级倒置。
名称:: |
sched_get_priority_max sched_get_priority_min |
功能: |
获得系统支持的线程优先权的最大和最小值 |
头文件: |
#include <pthread.h> |
函数原形: |
int sched_get_priority_max(int policy); int sched_get_priority_min(int policy); |
参数: |
policy 系统支持的线程优先权的最大和最小值 |
返回值: |
若成功返回0,若失败返回-1。 |
下面是上面几个函数的程序例子:
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
void *child_thread(void *arg)
{
int policy;
int max_priority,min_priority;
struct sched_param param;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED);
pthread_attr_getinheritsched(&attr,&policy);
if(policy==PTHREAD_EXPLICIT_SCHED)
printf("Inheritsched:PTHREAD_EXPLICIT_SCHED\n");
if(policy==PTHREAD_INHERIT_SCHED)
printf("Inheritsched:PTHREAD_INHERIT_SCHED\n");
pthread_attr_setschedpolicy(&attr,SCHED_RR);
pthread_attr_getschedpolicy(&attr,&policy);
if(policy==SCHED_FIFO)
printf("Schedpolicy:SCHED_FIFO\n");
if(policy==SCHED_RR)
printf("Schedpolicy:SCHED_RR\n");
if(policy==SCHED_OTHER)
printf("Schedpolicy:SCHED_OTHER\n");
sched_get_priority_max(max_priority);
sched_get_priority_min(min_priority);
printf("Max priority:%u\n",max_priority);
printf("Min priority:%u\n",min_priority);
param.sched_priority=max_priority;
pthread_attr_setschedparam(&attr,¶m);
printf("sched_priority:%u\n",param.sched_priority);
pthread_attr_destroy(&attr);
}
int main(int argc,char *argv[ ])
{
pthread_t child_thread_id;
pthread_create(&child_thread_id,NULL,child_thread,NULL);
pthread_join(child_thread_id,NULL);
}
六、栈大小(stack size)
当系统中有很多线程时,可能需要减小每个线程栈的默认大小,防止进程的地址空间不够用;当线程调用的函数会分配很大的局部变量或者函数调用层次很深时,可能需要增大线程栈的默认大小。函数pthread_attr_getstacksize和 pthread_attr_setstacksize被提供。
int pthread_attr_getstacksize ( const pthread_attr_t *attr, size_t *size );
int pthread_attr_setstacksize ( pthread_attr_t *attr, size_t size );
函数pthread_attr_getstack和pthread_attr_setstack函数可以同时操作栈地址和栈大小两个属性:
int pthread_attr_getstack ( const pthread_attr_t *attr, void **stackaddr, size_t *size );
int pthread_attr_setstack ( pthread_attr_t *attr, void *stackaddr, size_t size );
七、栈保护区大小(stack guard size)
在线程栈顶留出一段空间,防止栈溢出。当栈指针进入这段保护区时,系统会发出错误,通常是发送信号给线程。该属性默认值是PAGESIZE大小,该属性被设置时,系统会自动将该属性大小补齐为页大小的整数倍。当改变栈地址属性时,栈保护区大小通常清零。
int pthread_attr_getguardsize ( const pthread_attr_t *attr, size_t *guardsize );
int pthread_attr_setguardsize ( pthread_attr_t *attr, size_t guardsize );
八、争用范围(scope)
建立线程的争用范围(PTHREAD_SCOPE_SYSTEM 或 PTHREAD_SCOPE_PROCESS)。使用 PTHREAD_SCOPE_SYSTEM 时,此线程将与系统中的所有线程进行竞争。使用 PTHREAD_SCOPE_PROCESS 时,此线程将与进程中的其他线程进行竞争。这个又叫绑定状态,PTHREAD_SCOPE_SYSTEM(绑定的)和PTHREAD_SCOPE_PROCESS(非绑定的)。具有不同范围状态的线程可以在同一个系统甚至同一个进程中共存。进程范围只允许这种线程与同一进程中的其他线程争用资源,而系统范围则允许此类线程与系统内的其他所有线程争用资源。对于Solaris系统,实际上,从 Solaris 9 发行版开始,系统就不再区分这两个范围。
int pthread_attr_getscope(const pthread_attr_t *restrict attr, int *restrict contentionscope);
int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
(9)线程并行级别(concurrency)
应用程序使用 pthread_setconcurrency() 通知系统其所需的并发级别。
int pthread_getconcurrency(void);
int pthread_setconcurrency(int new_level);
例:创建优先级为50的线程。
pthread_attr_t attr;
struct sched_param param;
pthread_attr_init(&attr);
pthread_attr_setschedpolicy(&attr, SCHED_RR);
param.sched_priority = 50;
pthread_attr_setschedparam(&attr, ¶m);
pthread_create(&threadid, &attr, &threadfunc, NULL);
pthread_attr_destroy(&attr);
发表评论
-
【转载】如何用Linux的命令正确识别cpu的个数和核数
2013-08-20 11:40 1881如何在Linux下cpu的个数和核数呢?googel了一下 ... -
redis2.6.9源码学习---ziplist
2013-05-16 12:05 1752ziplist相比之前分析的z ... -
redis2.6.9源码学习---zipmap
2013-05-02 17:03 2217在看此文件源码之前,先看到此文件头部的英文注释,以下是本人 ... -
redis2.6.9源码学习---zipmap
2013-05-02 14:22 0<div class="iteye-blog- ... -
非阻塞connect编程
2013-04-08 17:08 1712非阻塞模式有3种用途 1.三次握手同时做其他的处理。co ... -
C库需要注意的函数
2013-03-08 16:50 2183本文转自:http://hub.opensolaris ... -
redis2.6.9源码学习---adlist
2013-02-07 16:27 1273源码adlist.c adlist.h,先来看看adlist ... -
redis2.6.9源码学习---dict
2013-02-07 11:59 1459redis的hashtable------dict.c ... -
redis2.6.9源码学习---Big_Endian&Little_Endian
2013-02-04 10:50 2723在阅读redis源码/src/endianconv.c时遇 ... -
linux c学习笔记----SCTP基础客户/服务编程(setsockopt,sctp_sendmsg等)
2013-01-15 17:53 19652在编程之前先了解一下sctp套接字选项 setsoc ... -
linux c学习笔记----UDP基础客户/服务编程(sendto,recvfrom)
2013-01-14 18:01 53689sendto(经socket传送数据) 相关函数 ... -
linux c学习笔记----select函数详解
2013-01-11 17:25 20239select系统调用是用来让我们的程序监视多个文件句柄(fil ... -
linux c学习笔记----TCP基础客户/服务编程(socket,bind等)
2013-01-10 17:29 28393socket(建立一个socket通信) 相关 ... -
linux c学习笔记----互斥锁属性
2013-01-05 18:37 11470互斥锁属性 使用互斥锁(互斥)可以使线程按顺序执行。通 ... -
linux c学习笔记----线程同步
2012-12-28 17:49 29871.互斥量 互斥变量用pthead_mutex_t数据类 ... -
linux c学习笔记----线程创建与终止
2012-12-14 17:32 13675进程原语 线程原语 描述 fork p ... -
linux c学习笔记----共享内存(shmget,shmat,shmdt,shmctl)
2012-12-11 18:08 36663shmgetint shmget(key_t key, siz ... -
linux c学习笔记----消息队列(ftok,msgget,msgsnd,msgrcv,msgctl)
2012-12-07 17:46 33304ftok() #include <sys/t ... -
linux信号列表
2012-11-30 16:40 2770我们运行如下命令, ... -
linux c学习笔记----信号(sigaction,sigaddset,sigprocmask)
2012-11-30 16:23 15214sigaction(查询 ...
相关推荐
【linux学习笔记--17】POSIX IPC——消息队列.doc 【linux学习笔记--18】POSIX IPC——信号量.doc 【linux学习笔记--19】POSIX IPC——共享内存.doc 【linux学习笔记-10】Linux进程相关系统调用(三).doc 【linux...
【linux学习笔记-1】使用GDB调试简单的用户程序 【linux学习笔记-2】父子进程共享文件描述符 【linux学习笔记-3】文件操作(stat,chmod,umask) 【linux学习笔记-4】进程扇和进程链 【linux学习笔记-5】编写自己的ls...
armlinux学习笔记--IIS音频驱动程序分析
ARM linux学习笔记--IIS音频驱动程序分析整理
Python学习笔记--皮大庆,非常适合零基础入门。对应英文版本《How to think like a computer scientist》
c语言学习笔记-简易版扫雷
认真学习的人从国内某个培训机构上课时,记录的C语言学习笔记,其中包含所有的C语言基本语法,文章中对指针、字符操作有好多介绍,整理的很全面,并且有图示哦!! 相信对大家学习C语言会有很大帮助!!!
C语言学习笔记-详细而全面的基础教程.pdf
JXW的C语言学习笔记-待查
写得蛮好的linux学习笔记--linux目录架构
linux内核移植 笔记--移植指令 linux内核移植 笔记--移植指令 linux内核移植 笔记--移植指令
armlinux学习笔记--触摸屏驱动程序分析.pdf
Linux课程学习笔记 -韩顺平 包含c/c++/python/java 专项 面试题 PDF PPT 笔记 面试题 (百度网盘链接 永久有效) 自学,做笔记,复习可用
armlinux学习笔记--IIS音频驱动程序分析(2).pdf
Linux学习笔记Linux学习笔记Linux学习笔记Linux学习笔记
armlinux学习笔记--IIS音频驱动程序分析(2)资料.pdf
linux学习笔记-01linux操作基础
找到的一篇写的很好的linux学习笔记 不敢独享 大家有兴趣的就下下
Linux学习笔记-超全总结值得一看(带标签目录),共154页。
python爬虫学习笔记-scrapy框架(1) python scrapy 爬虫 python爬虫学习笔记-scrapy框架(1) python scrapy 爬虫 python爬虫学习笔记-scrapy框架(1) python scrapy 爬虫 python爬虫学习笔记-scrapy框架(1) python ...