In concurrent programming, a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true. Monitors also have a mechanism for signaling other threads that their condition has been met. A monitor consists of a mutex (lock) object and condition variables. A condition variable is basically a container of threads that are waiting for a certain condition. Monitors provide a mechanism for threads to temporarily give up exclusive access in order to wait for some condition to be met, before regaining exclusive access and resuming their task.

Another definition of monitor is a thread-safe classobject, or module that uses wrapped mutual exclusion in order to safely allow access to a method or variable by more than one thread. The defining characteristic of a monitor is that its methods are executed with mutual exclusion: At each point in time, at most one thread may be executing any of its methods. By using one or more condition variables it can also provide the ability for threads to wait on a certain condition (thus using the above definition of a "monitor"). For the rest of this article, this sense of "monitor" will be referred to as a "thread-safe object/class/module".

Condition variables[edit]

Problem statement[edit]

For many applications, mutual exclusion is not enough. Threads attempting an operation may need to wait until some condition P holds true. A busy waiting loop

   while not( P ) do skip

will not work, as mutual exclusion will prevent any other thread from entering the monitor to make the condition true.

Spin-waiting[edit]

One naive approach to achieve synchronization, as alluded to above, is to use "spin-waiting", in which a mutex is used to protect the critical sections of code and busy-waiting is still used, with the lock being acquired and released in between each busy-wait check.

global RingBuffer queue; // A thread-unsafe ring-buffer of tasks.
global Lock queueLock; // A mutex for the ring-buffer of tasks. // Method representing each producer thread's behavior:
public method producer(){
while(true){
task myTask=...; // Producer makes some new task to be added. queueLock.acquire(); // Acquire lock for initial busy-wait check.
while(queue.isFull()){ // Busy-wait until the queue is non-full.
queueLock.release();
// Drop the lock temporarily to allow a chance for other threads
// needing queueLock to run so that a consumer might take a task.
queueLock.acquire(); // Re-acquire the lock for the next call to "queue.isFull()".
} queue.enqueue(myTask); // Add the task to the queue.
queueLock.release(); // Drop the queue lock until we need it again to add the next task.
}
} // Method representing each consumer thread's behavior:
public method consumer(){
while(true){
queueLock.acquire(); // Acquire lock for initial busy-wait check.
while (queue.isEmpty()){ // Busy-wait until the queue is non-empty.
queueLock.release();
// Drop the lock temporarily to allow a chance for other threads
// needing queueLock to run so that a producer might add a task.
queueLock.acquire(); // Re-acquire the lock for the next call to "queue.isEmpty()".
}
myTask=queue.dequeue(); // Take a task off of the queue.
queueLock.release(); // Drop the queue lock until we need it again to take off the next task.
doStuff(myTask); // Go off and do something with the task.
}
}

Monitor (synchronization)条件变量-安全对象的更多相关文章

  1. UNIX环境高级编程——线程同步之条件变量以及属性

    条件变量变量也是出自POSIX线程标准,另一种线程同步机制.主要用来等待某个条件的发生.可以用来同步同一进程中的各个线程.当然如果一个条件变量存放在多个进程共享的某个内存区中,那么还可以通过条件变量来 ...

  2. pThreads线程(三) 线程同步--条件变量

    条件变量(Condition Variables) 参考资料:http://game-lab.org/posts/posix-thread-cn/#5.1 条件变量是什么? 条件变量为我们提供了另一种 ...

  3. pthread中互斥量,锁和条件变量

    互斥量 #include <pthread.h> pthread_mutex_t mutex=PTHREAD_MUTEX_INTIIALIZER; int pthread_mutex_in ...

  4. 【Linux C 多线程编程】互斥锁与条件变量

    一.互斥锁 互斥量从本质上说就是一把锁, 提供对共享资源的保护访问. 1) 初始化: 在Linux下, 线程的互斥量数据类型是pthread_mutex_t. 在使用前, 要对它进行初始化: 对于静态 ...

  5. Linux的线程同步对象:互斥量Mutex,读写锁,条件变量

        进程是Linux资源分配的对象,Linux会为进程分配虚拟内存(4G)和文件句柄等 资源,是一个静态的概念.线程是CPU调度的对象,是一个动态的概念.一个进程之中至少包含有一个或者多个线程.这 ...

  6. node源码详解(七) —— 文件异步io、线程池【互斥锁、条件变量、管道、事件对象】

    本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource7 本博客同步在https://cnodejs.o ...

  7. js中, 用变量或对象作为if或其他条件的表达式

    源: 因为js是弱语言, 就体现在js的变量是弱类型的, 在js中所有变量类型声明都用var, 而在其他强类型语言中,如java/c,必须有强制类型转换和类型检查才能编译通过等, 但是: 弱语言也有优 ...

  8. 深入解析条件变量(condition variables)

    深入解析条件变量 什么是条件变量(condition variables) 引用APUE中的一句话: Condition variables are another synchronization m ...

  9. [development][C] 条件变量(condition variables)的应用场景是什么

    产生这个问题的起因是这样的: ‎[:] ‎<‎tong‎>‎ lilydjwg: 主线程要启动N个子线程, 一个局部变量作为把同样的参数传入每一个子线程. 子线程在开始的十行会处理完参数. ...

随机推荐

  1. FloatActionButton弹出菜单

    浮动按钮的弹出菜单动画 将几个按钮重叠摆放,使用ValueAnimator更新按钮的坐标实现. 布局 <FrameLayout android:layout_width="match_ ...

  2. HDU 1213 How Many Tables【并查集】

    解题思路:和畅通工程类似,问最后还剩下几个不连通的区域. How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  3. echarts的下载为excel小工具

    echarts自带有下载为图片的功能,但是没有下载为excel表格的功能,个人觉得下载为表格更为需要,所以写了个工具,此工具应用有局限性,只能用于柱形图和折线图,option的series要是如下形式 ...

  4. easyUI datagrid表头的合并

    图列: js代码 function initConfigTable(param){ $("#mulConfigureTableBox").empty(); $("#mul ...

  5. centos7安装anaconda之后报错:rpm: /home/wyl/anaconda3/lib/liblzma.so.5: version `XZ_5.1.2alpha' not found (required by /lib64/librpmio.so.3)

    1.报错 参考:https://stackoverflow.com/questions/47633870/rpm-lib64-liblzma-so-5-version-xz-5-1-2alpha-no ...

  6. done

  7. Lvs+heartbeat高可用高性能web站点的搭建

    这是我们公司在实际的生产环境当中使用的一套东西,希望对大家有所帮助(实际的公网ip,我已经做了相应的修改): 说明:每台服务器需要有两块网卡:eth0连接内网的交换机,用私网ip,实现服务器间内部访问 ...

  8. reac实现控制输入框字符长度

    reac实现控制输入框字符长度 代码思路:

  9. SpringBoot中打包设置,将配置文件打包在外部

    一.每次用maven的打包工具打包的时候 总是将配置文件一起打包进jar中!配置文件有点小修改就要重新打包很麻烦!!!!为了解决这一麻烦!找 了很多方法,下面的配置已经实现可用 我的项目目录结构如下 ...

  10. 百度语音识别服务 —— 语音识别 REST API 开发笔记

    http://blog.csdn.net/lw_power/article/details/51771267