APUE学习笔记——11 线程同步、互斥锁、自旋锁、条件变量
线程同步
a(B), b(A),b(B),c(A),c(B)。那么执行的过程就是AB都把i原值i=0copy到寄存器,进行自加,得到结果1,然后分别有把值1赋值到内存中的i,导致i结果为1.
互斥锁 Mutexes
#include <pthread.h>int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t*restrict attr);int pthread_mutex_destroy(pthread_mutex_t *mutex);Both return: 0 if OK, error number on failure
进行初始化,而不调用pthread_mutex_init函数。
#include <pthread.h>int pthread_mutex_lock(pthread_mutex_t *mutex);int pthread_mutex_trylock(pthread_mutex_t *mutex);int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,const struct timespec *restrict tsptr);int pthread_mutex_unlock(pthread_mutex_t *mutex);All return: 0 if OK, error number on failure
关于tsptr,它是一个时间点,比如我们要等待3min,则结果tsptr应该是当前时间加上3min。
死锁与避免死锁
pthread_mutex_lock(mutex);pthread_mutex_lock(mutex);pthread_mutex_unlock(mutex);pthread_mutex_unlock(mutex);
//thread Apthread_mutex_lock(mutex1);pthread_mutex_lock(mutex2);pthread_mutex_unlock(mutex2);pthread_mutex_unlock(mutex1);//thread Bpthread_mutex_lock(mutex2);pthread_mutex_lock(mutex1);pthread_mutex_unlock(mutex1);pthread_mutex_unlock(mutex2);
在pthread_mutex_lock(mutex2); 之前。
struct foo {int f_count;pthread_mutex_t f_lock;int f_id;/* ... more stuff here ... */};
读写锁
#include <pthread.h>int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,const pthread_rwlockattr_t*restrict attr);int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);Both return: 0 if OK, error number on failure
#include <pthread.h>int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);All return: 0 if OK, error number on failure
#include <pthread.h>int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);Both return: 0 if OK, error number on failure
#include <pthread.h>#include <time.h>int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrictrwlock,const struct timespec*restrict tsptr);int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrictrwlock,const struct timespec *restric ttsptr);
条件变量
#include <pthread.h>int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);int pthread_cond_destroy(pthread_cond_t *cond);Both return: 0 if OK, error number on failure
#include <pthread.h>int pthread_cond_wait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex);int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex,const struct timespec *restrict tsptr);
#include <pthread.h>int pthread_cond_signal(pthread_cond_t *cond);int pthread_cond_broadcast(pthread_cond_t *cond);Both return: 0 if OK, error number on failure
#include <pthread.h>struct msg {struct msg *m_next;/* ... more stuff here ... */};struct msg *workq;pthread_cond_t qready = PTHREAD_COND_INITIALIZER;pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;
voidprocess_msg(void){struct msg *mp;for (;;) {pthread_mutex_lock(&qlock);while (workq == NULL)pthread_cond_wait(&qready, &qlock);mp = workq;workq = mp->m_next;pthread_mutex_unlock(&qlock);/* now process the message mp */}}voidenqueue_msg(struct msg *mp){pthread_mutex_lock(&qlock);mp->m_next = workq;workq = mp;pthread_mutex_unlock(&qlock);pthread_cond_signal(&qready);}
Spin lock 自旋锁
#include <pthread.h>int pthread_spin_init(pthread_spinlock_t *lock,intpshared);int pthread_spin_destroy(pthread_spinlock_t *lock);Both return: 0 if OK, error number on failure
#include <pthread.h>int pthread_spin_lock(pthread_spinlock_t *lock);int pthread_spin_trylock(pthread_spinlock_t *lock);int pthread_spin_unlock(pthread_spinlock_t *lock);All return: 0 if OK, error number on failure
Barriers 计数锁
#include <pthread.h>int pthread_barrier_init(pthread_barrier_t *restrictbarrier,const pthread_barrierattr_t *restrict attr,unsigned int count);int pthread_barrier_destroy(pthread_barrier_t *barrier);Both return: 0 if OK, error number on failure
#include <pthread.h>int pthread_barrier_wait(pthread_barrier_t *barrier);Returns: 0 orPTHREAD_BARRIER_SERIAL_THREADif OK, error number on failure
APUE学习笔记——11 线程同步、互斥锁、自旋锁、条件变量的更多相关文章
- APUE 学习笔记(八) 线程同步
1. 进程的所有信息对该进程内的所有线程都是共享的 包括 可执行的程序文本.程序全局内存.堆内存以及文件描述符 线程包含了表示进程内执行环境必需的信息,包括线程ID.寄存器值.栈.调度优先级和策略.信 ...
- APUE学习笔记——11 线程基础
线程标识 线程由线程号进行标识.线程号仅在线程所属的进程环境中有效.也就是说属于不同进程的两个线程可能线程号一样. 线程标识用结构体pthread_t tid表示.与线程Id相关的函数如下: 比较两个 ...
- APUE学习笔记6——线程和线程同步
1 概念 线程是程序执行流的最小单元.线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的 ...
- python学习笔记11 ----线程、进程、协程
进程.线程.协程的概念 进程和线程是操作系统中两个很重要的概念,对于一般的程序,可能有若干个进程,每一个进程有若干个同时执行的线程.进程是资源管理的最小单位,线程是程序执行的最小单位(线程可共享同一进 ...
- linux学习笔记之线程同步机制
一.基础知识. 1:线程同步机制:互斥量,读写锁,条件变量,自旋锁,屏障. 1,互斥量:每个进程访问被互斥量保护的资源时,都需要先对互斥量进行判断. 1)互斥量重要属性:进程共享属性,健壮属性,类型属 ...
- Linux学习笔记21——线程同步的两种方式
一 用信号量同步 1 信号量函数的名字都以sem_开头,线程中使用的基本信号量函数有4个 2 创建信号量 #include<semaphore.h> int sem_init(sem_t ...
- 第8章 用户模式下的线程同步(4)_条件变量(Condition Variable)
8.6 条件变量(Condition Variables)——可利用临界区或SRWLock锁来实现 8.6.1 条件变量的使用 (1)条件变量机制就是为了简化 “生产者-消费者”问题而设计的一种线程同 ...
- C#学习笔记之线程 - 同步上下文
同步上下文(Synchronization Contexts) 手动使用锁的一个替代方案是去声明锁.通过派生ContextBoundObject和应用Synchronization属性,你告诉CLR自 ...
- C++11并发学习之三:线程同步(转载)
C++11并发学习之三:线程同步 1.<mutex> 头文件介绍 Mutex又称互斥量,C++ 11中与 Mutex 相关的类(包括锁类型)和函数都声明在 <mutex> 头文 ...
随机推荐
- mysql5.6创建索引导致锁表阻塞查询
结论:添加索引时,若果有对该表的慢查询,会导致索引添加延时等待 添加索引语句:alter table tb_name add index idx_xx(col_name); 执行添加索引的SQ ...
- 20145321 《Java程序设计》第4周学习总结
20145321 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 1.继承共同行为: 继承基本上就是避免多个类间重复定义的行为. Pull Up ...
- Mac系统安装MyEclipse
参考链接 http://blog.csdn.net/jin_kwok/article/details/51925523
- Elasticsearch之IKAnalyzer的过滤停止词
它在哪里呢? 非常重要! [hadoop@HadoopMaster custom]$ pwd/home/hadoop/app/elasticsearch-2.4.3/plugins/ik/config ...
- SQL映射文件-----MySQL关系映射【1对1,1对多,多对多】
SSM框架下,mapper.xml 中 association 标签和 collection 标签的使用 当数据库中表与表之间有关联时,在对数据库进行操作时,就不只是针对某一张表了,需要联表查询 My ...
- Linux 安装rabbitmq 遇到的一些问题
Linux下防火墙开启相关端口及查看已开启端口 https://www.cnblogs.com/pizitai/p/6518987.htmlhttps://www.cnblogs.com/blog-y ...
- python2.7.10 VS2015编译方法
打开 Python-2.7.10\PCbuild目录 然后设置只编译python和pythoncore: 好了,编译试一试. 出现了好几个错误.由于 VS2015 取消了 timezone 的定义,改 ...
- poj 1050 To the Max 最大子矩阵和 经典dp
To the Max Description Given a two-dimensional array of positive and negative integers, a sub-rect ...
- MySQL索引底层实现
一.定义 索引定义:索引(Index)是帮助MySQL高效获取数据的数据结构.本质:索引是数据结构. 二.B-Tree m阶B-Tree满足以下条件: 每个节点至多可以拥有m棵子树. 根节点,只有至少 ...
- Nginx 从0开始学
作为一个 nginx 的初学者记录一下从零起步的点滴. 基本概念 Nginx 最常的用途是提供反向代理服务,那么什么反向代理呢?正向代理相信很多大陆同胞都在这片神奇的土地上用过了,原理大致如下图: 代 ...