线程属性总结

今天面试那哥们问起线程属性,me竟然就说出了一个,囧大哭

学习:http://blog.csdn.net/zsf8701/article/details/7842392

            http://blog.csdn.net/jxhnuaa/article/details/3254299

            http://blog.sina.com.cn/s/blog_9bd573450101hgdr.html


int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

在线程建立时,第二参数是设置线程的属性,当为NULL时,设置成系统默认属性:非邦定、非分离、缺省1M的堆栈、与父进程同样级别的优先级。


/usr/include/pthread.h 包含 bits/pthreadtypes.hunion pthread_attr_t
{char __size[__SIZEOF_PTHREAD_ATTR_T];long int __align;
};typedef union pthread_attr_t pthread_attr_t;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;

从上面的结构可以看到线程都有些什么属性,下面一些函数来设置线程属性:


       #include <pthread.h>int pthread_attr_init(pthread_attr_t *attr);int pthread_attr_destroy(pthread_attr_t *attr);

功能:initialize and destroy thread attributes object.

Calling pthread_attr_init() on a thread attributes object that has already been initialized results in undefined behavior.

       When  a  thread  attributes  object is no longer required, it should be destroyed using the pthread_attr_destroy() function.  Destroying a thread attributes
       object has no effect on threads that were created using that object.


On success, these functions return 0; on error, they return a nonzero error number.


       #include <pthread.h>int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate);

功能:set/get detach state attribute in thread attributes object.

线程的分离状态决定一个线程以什么样的方式来终止自己
PTHREAD_CREATE_DETACHED
分离线程没有被其他的线程所等待,自己运行结束了,线程也就终止了,马上释放系统资源。应该根据自己的需要,选择适当的分离状态
PTHREAD _CREATE_JOINABLE
线程的默认属性是非分离状态,这种情况下,原有的线程等待创建的线程结束。只有当pthread_join()函数返回时,创建的线程才算终止,才能释放自己占用的系统资源。

如果设置一个线程为分离线程,而这个线程运行又非常快,它很可能在pthread_create函数返回之前就终止了,它终止以后就可能将线程号和系统资源移交给其他的线程使用,这样调用pthread_create的线程就得到了错误的线程号。要避免这种情况可以采取一定的同步措施,最简单的方法之一是可以在被创建的线程里调用pthread_cond_timewait函数,让这个线程等待一会儿,留出足够的时间让函数pthread_create返回。设置一段等待时间,是在多线程编程里常用的方法。但是注意不要使用诸如wait()之类的函数,它们是使整个进程睡眠,并不能解决线程同步的问题。


       #include <pthread.h>int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);

功能:set/get scheduling policy attribute in thread attributes object

POSIX标准指定了三种调度策略:先入先出策略 (SCHED_FIFO)、循环策略 (SCHED_RR) 和自定义策略 (SCHED_OTHER)。SCHED_FIFO 是基于队列的调度程序,对于每个优先级都会使用不同的队列。SCHED_RR 与 FIFO 相似,不同的是前者的每个线程都有一个执行时间配额。SCHED_FIFO 和 SCHED_RR 是对 POSIX Realtime 的扩展。SCHED_OTHER 是缺省的调度策略。
新线程默认使用 SCHED_OTHER 调度策略。线程一旦开始运行,直到被抢占或者直到线程阻塞或停止为止。
SCHED_FIFO
如果调用进程具有有效的用户 ID 0,则争用范围为系统 (PTHREAD_SCOPE_SYSTEM) 的先入先出线程属于实时 (RT) 调度类。如果这些线程未被优先级更高的线程抢占,则会继续处理该线程,直到该线程放弃或阻塞为止。对于具有进程争用范围 (PTHREAD_SCOPE_PROCESS)) 的线程或其调用进程没有有效用户 ID 0 的线程,请使用 SCHED_FIFO,SCHED_FIFO 基于 TS 调度类。
SCHED_RR
如果调用进程具有有效的用户 ID 0,则争用范围为系统 (PTHREAD_SCOPE_SYSTEM)) 的循环线程属于实时 (RT) 调度类。如果这些线程未被优先级更高的线程抢占,并且这些线程没有放弃或阻塞,则在系统确定的时间段内将一直执行这些线程。对于具有进程争用范围 (PTHREAD_SCOPE_PROCESS) 的线程,请使用 SCHED_RR(基于 TS 调度类)。此外,这些线程的调用进程没有有效的用户 ID 0。


       #include <pthread.h>int pthread_attr_setschedparam(pthread_attr_t *attr,const struct sched_param *param);int pthread_attr_getschedparam(pthread_attr_t *attr,struct sched_param *param);

功能:set/get scheduling parameter attributes in thread attributes object

           struct sched_param {int sched_priority;     /* Scheduling priority */};


       #include <pthread.h>int pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched);int pthread_attr_getinheritsched(pthread_attr_t *attr,int *inheritsched);

功能:set/get inherit scheduler attribute in thread attributes object

PTHREAD_EXPLICIT_SCHED
PTHREAD_INHERIT_SCHED
前者表示使用结构pthread_attr_t指定的调度算法,后者表示继承父线程使用的调度算法,默认为前者


       #include <pthread.h>int pthread_attr_setscope(pthread_attr_t *attr, int scope);int pthread_attr_getscope(pthread_attr_t *attr, int *scope);

功能:设置线程作用域

PTHREAD_SCOPE_SYSTEM
PTHREAD_SCOPE_PROCESS
前者表示在整个系统内竞争CPU资源,后者表示在同一进程内竞争CPU资源,默认为前者


       #include <pthread.h>int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);int pthread_attr_getguardsize(pthread_attr_t *attr, size_t *guardsize);

功能: set/get guard size attribute in thread attributes object

在线程栈顶留出一段空间,防止栈溢出。
当栈指针进入这段保护区时,系统会发出错误,通常是发送信号给线程。
该属性默认值是PAGESIZE大小,该属性被设置时,系统会自动将该属性大小补齐为页大小的整数倍。
当改变栈地址属性时,栈保护区大小通常清零。


       #include <pthread.h>int pthread_attr_setstack(pthread_attr_t *attr,void *stackaddr, size_t stacksize);int pthread_attr_getstack(pthread_attr_t *attr,void **stackaddr, size_t *stacksize);

功能:获取/设置线程栈及其大小

stackaddr  should  point  to the lowest addressable byte of a buffer of stacksize bytes that was allocated by the caller.  The pages of the allocated buffer should be both readable and writable.


	#include <pthread.h>int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr);int pthread_attr_getstackaddr(pthread_attr_t *attr, void **stackaddr);#include <pthread.h>int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);


实例见:man pthread_attr_init








本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/508704.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

单链表面试经典问题

/************************************************** http://www.cnblogs.com/lifuqing/archive/2011/08/20/List.html http://www.cnblogs.com/wenjiang/p/3310233.html 链表经典问题汇总:http://blog.csdn.net/vividonly/article/details/6673758 链表有关的常见面试题:htt…

Linux SO_KEEPALIVE属性,心跳

对于面向连接的TCP socket,在实际应用中通常都要检测对端是否处于连接中,连接端口分两种情况: 1、连接正常关闭,调用close() shutdown()连接优雅关闭,send与recv立马返回错误,select返回SOCK_ERR; 2、连接的对端异常关闭,比如网络断掉,突然断电. 对于第二种情况,判断连接是否断…

Linux下ARM开发环境搭建

本人的系统环境&#xff1a;Linux ubuntu 3.8.0-35-generic #50-Ubuntu SMP Tue Dec 3 01:25:33 UTC 2013 i686 i686 i686 GNU/Linux 1、安装skyeye sudo apt-get install skyeye s kyeye -h可以看到skyeye的版本号为1.2.5也可以到http://sourceforge.jp/projects/sfnet_skyeye…

cygwin开发环境搭建与apt-cyg的应用

1、Cygwin安装 http://www.cygwin.com/下载安装工具 具体安装过程参照http://jingyan.baidu.com/article/6b97984d83dfe51ca2b0bf0e.html 2、Cygwin一些设置 打开Cygwin终端,右击打开 Options...选项 Text可以设置字体的一些属性,如大小、编码,Locale 选择C, Character set 选择…

CentOS下升级python版本

源码安装python 安装python源码所依赖的工具及依赖的库 yum install -y make gcc gcc-c yum install -y bzip2 bzip2-devel yum install zlib-devel openssl openssl-devel -y yum install -y make xz下载安装python源码 从官方网站或者华为镜像源下载所有需的源码包&#xff0…

linux下confstr与uname函数_获取C库与内核信息

#include <stdio.h> #include <sys/utsname.h> //unameint main(int argc, char **argv[]) {struct utsname u;if (uname(&u) ! -1) {printf("获取当前内核的名称和信息如下\n""sysname:%s\n""nodename:%s\n""release:%s\…

linux下getrlimit与sysconf函数

#include <stdio.h> #include <sys/time.h> #include <sys/resource.h>int main(int argc, char *argv[]) {struct rlimit nofile_rlmt;if (getrlimit(RLIMIT_NOFILE, &nofile_rlmt) ! -1) {printf("获取进程最大能打开的文件描述符个数信息:\n&quo…

Linux下environ环境变量操作函数

#include <stdio.h>int main(int argc,char *argv[],char **envptr) {int i0;for(i0; envptr[i]!NULL; i)printf("%s\n",envptr[i]);return 0; } main函数是程序的入口函数,int main(int argc,char *argv[]); argc是程序参数的个数,argv保存参数 与下边的程…

Linux网络编程--聊天室客户端程序

聊天室客户端程序 #define _GNU_SOURCE 1 #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <assert.h> #include <stdio.h> #include <unistd.h> #include <string.h&…

二叉树学习之二叉查找树

写在前面的话 最近接到几个大学同学研究生毕业不是签华为就是签百度,本人取经得到:操作系统、数据结构与算法、网络编程与数据库是面试中利器。想想自己工作2.5年月薪还不到10K,过着苦逼的码农生活,而他们一出校门就是大放光芒(最起码进入的公司就让人觉得牛逼哄哄的).本人痛定…

二叉树学习之非递归遍历

二叉树递归遍历可谓是学过数据结构的同仁都能想一下就能写出来,但在应聘过程我们常常遇到的是写出一个二叉树非递归遍历函数,接着上篇文章写二叉树的非递归遍历,先难后易,一步一步的来. 先上代码: #include "binarytree.h" #include <stack> #include <queu…

二叉树学习之堆排序

认识堆是从堆排序开始的 二叉堆是完全二叉树或者是近似完全二叉树,堆存储在数组中: 根结点下标为0时,下标为n的元素的子结点下标分别为2*n1,2*n2,其父结点下标为(n-1)/2 二叉堆的特性: 1、父结点的键值总是>(<)任何一个子结点的键值 2、每个结点的左右子树都是二叉堆…

步入github世界

在源码的世界里&#xff0c;越来越多的优秀源码涌现&#xff0c;开源的世界不但代表他的优秀&#xff0c;也代表了他优秀的传播途径。 https://github.com/ github自从2008年现世&#xff0c;可谓是后来者居上。开源代码的公开库&#xff0c;优秀程序员的博客园&#xff0c;热心…

libevent学习__学习历程总结

The libevent API provides a mechanism to execute a callback function when a specific event occurs on a file descriptor or after a timeout has been reached. Furthermore, libevent also support callbacks due to signals or regular timeouts.环境搭建 下载: http:…

redis☞ python客户端

安装 https://pypi.python.org/pypi/redis/ https://github.com/andymccurdy/redis-py 参照官网,安装命令 sudo pip install redis 或者 sudo easy_install redis 亦或 源码包执行sudo python setup.py install实例 >>> import redis >>> r redis.Redis(ho…

Catalan数应用

Catalan数应用 Catalan数应用原理卡特兰数经典应用括号化买票找零组合数与阶乘计算 卡特兰数又称卡塔兰数&#xff0c;是组合数学中一个常出现在各种计数问题中的数列。由以比利时的数学家欧仁查理卡塔兰 (1814–1894)命名。 其前几项为 : 1, 2, 5, 14, 42, 132, 429, 1430,…

统计整数n的二进制表示中1的个数

(1)逐位判断&#xff08;位运算&#xff09;int counter_ones(unsigned n){int counter 0;While (n) {counter n&1;n >>1;}return counter;}(2)一个整型不为0&#xff0c;那么二进制表示时&#xff0c;至少包含一位1。如果整数减去1&#xff0c;那么最右边的1变成0…

百度2015校园招聘软件开发笔试题及答案

简单题&#xff08;本题共30分&#xff09; 请简述Tcp-ip的3次握手以及4次挥手过程&#xff1f;并解释为何关闭连接需要4次挥手(10分) 详细答案参见TCP/IP协议三次握手与四次握手流程解析 TCP三次握手、四次挥手过程如下: 通常情况下&#xff0c;一个正常的TCP连接&#xf…

python常见数据结构

Python中常见的数据结构可以统称为容器&#xff08;container&#xff09;。序列&#xff08;如列表和元组&#xff09;、映射&#xff08;如字典&#xff09;以及集合&#xff08;set&#xff09;是三类主要的容器。 一、序列&#xff08;列表、元组和字符串&#xff09; 序列…

理解‘*‘,‘*args‘,‘**‘,‘**kwargs‘

原文Understanding ‘*’, ‘*args’,’**‘and’**kwargs’ 刚开始学习python的时候&#xff0c;对有关args,kwargs,*和**的使用感到很困惑。相信对此感到疑惑的人也有很多。我打算通过这个帖子来排解这个疑惑(希望能减少疑惑)。 让我们通过以下5步来理解&#xff1a; 通过…