APUE学习之多线程编程(三):线程属性、同步属性
#include <pthread.h>
int pthread_attr_init(pthread_attr_t *attr)
int pthread_attr_destroy(pthread_attr_t *attr)
线程属性有四个:
#include <pthread.h>
int pthread_attr_getdetachstate(const pthread_attr_t *restrict attr, int *detachstate);
int pthread_attr_setdetachstate(const pthread_attr_t *attr, int *detachstate);
例子:
#include "apue.h"
#include <pthread.h> int makethread(void *(*fn)(void *), void *arg)
{
int err;
pthread_t tid;
pthread_attr_t attr; err = pthread_attr_init(&attr); if (err != )
{
return err;
} err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); if (err == )
{
err = pthread_create(&tid, &attr, fn, arg);
} pthread_attr_destroy(&attr);
return err;
}
#include <pthread.h>
int pthread_attr_getstack(const pthread_attr_t *restrict attr, void **restrick stackaddr, size_t *restrict stacksize)
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
如果线程栈的虚地址空间用完了,那可以使用malloc或者mmap来为可替代的栈分配空间,stackaddr为栈的最低内存地址。
#include <pthread.h>
int pthread_attr_getstacksize(const pthread_attr_t *restrict attr, size_t *restrict stacksize)
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t *stacksize)
线程属性guardsize控制着线程栈末尾之后用以避免栈溢出的扩展内存的大小。
#include <phtread.h>
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)
二、互斥量属性
#include <pthread.h>
int pthread_mutexattr_init(pthread_mutexattr *attr)
int pthread_mutexattr_destroy(pthread_mutexattr *attr)
互斥量属性中值得注意的两个属性:进程共享属性,类型属性
#include <pthread.h>
int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared)
int pthread_mutexattr_setpshared(const pthread_muteattr_t *attr, int pshared)
类型属性控制住互斥量的锁定特性,值得注意的是其中的PTHREAD_MUTEX_RECURSIVE类型,此类型允许同一线程在互斥量解锁之前对该互斥量进行多次加锁。递归互斥量维护锁的基数,在解锁次数和加锁次数不相同的情况下,不解锁。
#include <pthread.h>
int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr, int *restrict type)
int pthread_mutexattr_settype(pthread_mutexattr *attr, int type)
三、读写锁属性
#include <pthread.h>
int pthread_rwlockattr_init(pthread_rwlockattr_t *attr)
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)
读写锁唯一属性是进程共享属性,与互斥量的进程共享属性相同。
#include <pthread.h>
int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict attr, int *restrict pshared)
int pthread_rwlockattr_setpshared(const pthread_rwlockattr_t *attr, int * pshared)
四、条件变量属性
#include <pthread.h>
int pthread_condattr_init(pthread_condattr_t *attr)
int pthread_condattr_destroy(pthread_condattr_t *attr)
条件变量支持进程共享属性和时钟属性,其中进程共享属性与互斥量的进程共享属性相同。
#include <pthread.h>
int pthread_condattr_getpshared(const pthread_condattr_t *restrict attr, int *restrict pshared)
int pthread_condattr_setpshared(const pthread_condattr_t *attr, int pshared)
时钟属性控制pthread_cond_timedwait函数的超时参数tsptr采用的是哪个时钟。
#include <pthread.h>
int pthread_condattr_getclock(const pthread_condattr_t *restrict attr, clockid_t *restrict clock_id)
int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
APUE学习之多线程编程(三):线程属性、同步属性的更多相关文章
- APUE学习之多线程编程(二):线程同步
为了保证临界资源的安全性和可靠性,线程不得不使用锁,同一时间只允许一个或几个线程访问变量.常用的锁有互斥量,读写锁,条件变量 一.互斥量 互斥量是用pthrea ...
- APUE学习之多线程编程(一):线程的创建和销毁
一.线程标识 和每个进程都有一个进程ID一样,每个线程也有一个线程ID,线程ID是以pthread_t数据类型来表示的,在Linux中,用无符号长整型表示pthread_t,Solaris ...
- .NET面试题解析(07)-多线程编程与线程同步
系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 关于线程的知识点其实是很多的,比如多线程编程.线程上下文.异步编程.线程同步构造.GUI的跨线程访问等等, ...
- .NET面试题解析(07)-多线程编程与线程同步 (转)
http://www.cnblogs.com/anding/p/5301754.html 系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 关于线程的知识点其实 ...
- Python中的多线程编程,线程安全与锁(二)
在我的上篇博文Python中的多线程编程,线程安全与锁(一)中,我们熟悉了多线程编程与线程安全相关重要概念, Threading.Lock实现互斥锁的简单示例,两种死锁(迭代死锁和互相等待死锁)情况及 ...
- Python中的多线程编程,线程安全与锁(一)
1. 多线程编程与线程安全相关重要概念 在我的上篇博文 聊聊Python中的GIL 中,我们熟悉了几个特别重要的概念:GIL,线程,进程, 线程安全,原子操作. 以下是简单回顾,详细介绍请直接看聊聊P ...
- vc 基于对话框多线程编程实例——线程之间的通信
vc基于对话框多线程编程实例——线程之间的通信 实例:
- C#多线程编程实例 线程与窗体交互
C#多线程编程实例 线程与窗体交互 代码: public partial class Form1 : Form { //声明线程数组 Thread[] workThreads = ]; public ...
- Java并发编程:线程的同步
Java并发编程:线程的同步 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} J ...
随机推荐
- 深入浅出Java三大框架SSH与MVC的设计模式
现在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架:Struts2,Spring,Hibernate.似乎这些框架成为了一个人是否精通Java,是否会写J2EE程序的唯一事实标准和 ...
- Application Request Route实现IIS Server Farms集群负载详解
序言 随着公司业务的发展,后台业务就变的越来越多,然而服务器的故障又像月经一样,时不时的汹涌而至,让我们防不胜防.那么后台的高可用,以及服务器的处理能力就要做一个横向扩展的方案,以使后台业务持续的稳定 ...
- ES6之module
该博客原文地址:http://www.cnblogs.com/giggle/p/5572118.html 一.module概述 JavaScript一直没有模块体系,但是伴随着ES6的到来,modul ...
- 日常css技巧小结(2)-- inline-block带来的迷惑
一.问题描述 在平时布局中,inline-block使用的频率比很高,主要是因为可以让行标签设置宽高.我在布局过程中,发现了两个“问题”, 1行标签.display:inline-block之后的行标 ...
- Linux ERRNO
摘自Linux-3.18.20的头文件include/uapi/asm-generic/errno-base.h和include/uapi/asm-generic/errno.h: #define E ...
- 《HelloGitHub月刊》第09期
<HelloGitHub>第09期 兴趣是最好的老师,<HelloGitHub>就是帮你找到兴趣! 前言 转眼就到年底了,月刊做到了第09期,感谢大家一路的支持和帮助
- 【分布式】Zookeeper使用--开源客户端
一.前言 上一篇博客已经介绍了如何使用Zookeeper提供的原生态Java API进行操作,本篇博文主要讲解如何通过开源客户端来进行操作. 二.ZkClient ZkClient是在Zookeepe ...
- 你真的会玩SQL吗?让人晕头转向的三值逻辑
你真的会玩SQL吗?系列目录 你真的会玩SQL吗?之逻辑查询处理阶段 你真的会玩SQL吗?和平大使 内连接.外连接 你真的会玩SQL吗?三范式.数据完整性 你真的会玩SQL吗?查询指定节点及其所有父节 ...
- 深入学习jQuery特性操作
× 目录 [1]获取特性 [2]设置特性 [3]删除特性 前面的话 每个元素都有一个或者多个特性,这些特性的用途就是给出相应元素或者其内容的附加信息.操作特性的DOM方法主要有3个:getAttrib ...
- 查看Sql Server被锁的表以及解锁
查看被锁表: select spId from master..SysProcesses where db_Name(dbID) = '数据库名称' and spId <> @@SpId ...