互斥锁和条件变量(pthread)相关函数
互斥锁
#include <pthread.h> // 若成功返回0,出错返回正的Exxx值
// mptr通常被初始化为PTHREAD_MUTEX_INITIALIZER
int pthread_mutex_lock(pthread_mutex_t *mptr); int pthread_mutex_trylock(pthread_mutex_t *mptr); // pthread_mutex_lock 函数的非阻塞模式 int pthread_mutex_unlock(pthread_mutex_t *mptr);
条件变量:
#include <pthread.h> // 以下两个函数使用条件变量
// 若成功返回0,出错返回正的Exxx值
// cptr通常被初始化为PTHREAD_COND_INITIALIZE
int pthread_cond_signal(pthread_cond_t *cptr); // cptr指条件变量的类型 int pthread_cond_wait(pthread_cond_t *cptr, pthread_munex_t *mptr);
根据共享变量的状态来觉得是否要等待,而为了不永远等待下去所以必须要在lock/unlock
队中
#include <pthread.h> // 若成功返回0,出错返回正的Exxx值
int pthread_cond_broadcast(pthread_cond_t *cptr); // 唤醒在相应条件变量上的所有线程 int pthread_cond_timewait(pthread_cond_t *cptr, pthread_munex_t *mptr, // 允许线程设置一个阻塞时间限制
const struct timespec *abstime);
// abstime指的是绝对时间,而不是一个时间增量
// abstime通常调用gettimeofday获取当前时间,将其复制到timespec结构中,再加上期望的时间限制
互斥锁和条件变量的属性:
#include <pthread.h> // 使用非默认属性初始化互斥锁和条件变量,摧毁使用非默认属性初始化的互斥锁和条件变量
int pthread_mutex_init(pthread_mutex_t *mptr, const pthread_mutexattr_t *attr); int pthread_mutex_destory(pthread_mutex_t *mptr); int pthread_cond_init(pthread_cond_t *cptr, const pthread_condattr_t *attr); int pthread_cond_destory(pthread_cond_t *cptr); // 互斥锁的属性(pthread_mutexattr_t) 条件变量属性的数据类型(pthread_condattr_t)
// 属性的初始化和摧毁
// 若成功返回0,出错返回正的Exxx值
int pthread_mutexattr_t_init(pthread_mutexattr_t *attr); int pthread_mutexattr_t_destory(pthread_mutexattr_t *attr); int pthread_condattr_t_init(pthread_condattr_t *attr); int pthread_condattr_t_destory(pthread_condattr_t *attr); // 获取/设置互斥锁和条件变量的属性
// 若成功返回0,出错返回正的Exxx值
int pthread_mutexattr_t_getpshared(const pthread_mutexattr_t *attr, int *valptr); int pthread_mutexattr_t_setpshared(pthread_mutexattr_t *attr, int value); int pthread_condattr_t_getpshared(pthread_condattr_t *attr, int *valptr); int pthread_condattr_t_setpshared(pthread_condattr_t *attr, int value);
// value的取值(PTHREAD_PROCESS_PRIVATE或PTHREAD_PROCESS_SHARED(进程间共享属性))
互斥锁和条件变量(pthread)相关函数的更多相关文章
- Linux互斥锁、条件变量和信号量
Linux互斥锁.条件变量和信号量 来自http://kongweile.iteye.com/blog/1155490 http://www.cnblogs.com/qingxia/archive/ ...
- node源码详解(七) —— 文件异步io、线程池【互斥锁、条件变量、管道、事件对象】
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource7 本博客同步在https://cnodejs.o ...
- 非常精简的Linux线程池实现(一)——使用互斥锁和条件变量
线程池的含义跟它的名字一样,就是一个由许多线程组成的池子. 有了线程池,在程序中使用多线程变得简单.我们不用再自己去操心线程的创建.撤销.管理问题,有什么要消耗大量CPU时间的任务通通直接扔到线程池里 ...
- linux 线程的同步 二 (互斥锁和条件变量)
互斥锁和条件变量 为了允许在线程或进程之间共享数据,同步时必须的,互斥锁和条件变量是同步的基本组成部分. 1.互斥锁 互斥锁是用来保护临界区资源,实际上保护的是临界区中被操纵的数据,互斥锁通常用于保护 ...
- linux 互斥锁和条件变量
为什么有条件变量? 请参看一个线程等待某种事件发生 注意:本文是linux c版本的条件变量和互斥锁(mutex),不是C++的. mutex : mutual exclusion(相互排斥) 1,互 ...
- linux c 线程间同步(通信)的几种方法--互斥锁,条件变量,信号量,读写锁
Linux下提供了多种方式来处理线程同步,最常用的是互斥锁.条件变量.信号量和读写锁. 下面是思维导图: 一.互斥锁(mutex) 锁机制是同一时刻只允许一个线程执行一个关键部分的代码. 1 . ...
- 线程私有数据TSD——一键多值技术,线程同步中的互斥锁和条件变量
一:线程私有数据: 线程是轻量级进程,进程在fork()之后,子进程不继承父进程的锁和警告,别的基本上都会继承,而vfork()与fork()不同的地方在于vfork()之后的进程会共享父进程的地址空 ...
- 进程间通信机制(管道、信号、共享内存/信号量/消息队列)、线程间通信机制(互斥锁、条件变量、posix匿名信号量)
注:本分类下文章大多整理自<深入分析linux内核源代码>一书,另有参考其他一些资料如<linux内核完全剖析>.<linux c 编程一站式学习>等,只是为了更好 ...
- 【转载】同步和互斥的POSIX支持(互斥锁,条件变量,自旋锁)
上篇文章也蛮好,线程同步之条件变量与互斥锁的结合: http://www.cnblogs.com/charlesblc/p/6143397.html 现在有这篇文章: http://blog.cs ...
随机推荐
- 大数据学习——Storm学习单词计数案例
需求:计算单词在文档中出现的次数,每出现一次就累加一次 遇到的问题 这个问题是<scope>provided</scope>作用域问题 https://www.cnblogs. ...
- 【LeetCode】Add Two Numbers(两数相加)
这道题是LeetCode里的第2道题. 题目要求: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将 ...
- 13-数组的API方法遍历
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- PHP中file_put_contents追加和换行的实现方法
在PHP的一些应用中需要写日志或者记录一些信息,这样的话.可以使用fopen(),fwrite()以及 fclose()这些进行操作.也可以简单的使用file_get_contents()和file_ ...
- Spoj-TRNGL Make Triangle
Make Triangle Chayanika loves Mathematics. She is learning a new chapter geometry. While reading the ...
- mysql/oracle 连接参数中文变问号
jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf8&useSSL=true
- 使用putty上传下载文件(pscp)
putty作为ssh工具开源免费,简单易用.可是如何使用它来上传和下载文件呢?答案在于pscp. pscp下载地址:http://www.chiark.greenend.org.uk/~sgtatha ...
- 【Codeforces Round #502 (Div. 1 + Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9843032.html B:https://www.cnblogs.com/myx12345/p/9843050.html ...
- 玩转css样式选择器----利用padding实现元素等比缩放
- Laravel 表单及分页
控制器代码 //列表 public function index(){ //不带分页// $student = Student::get(); //带分页 $student = Student::pa ...