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.
}
}

最新文章

  1. 自制Unity小游戏TankHero-2D(3)开始玩起来
  2. 【faster-rcnn】训练自己的数据——修改图片格式、类别
  3. (转)json+flexgrid+jbox组合运用页面刷新<jsp>
  4. Windows游戏编程之从零开始d
  5. SQL Server 的 6 种隔离级别
  6. Spring Uploading Files
  7. 拆开Ceph看队列和线程
  8. 【编程技巧】NSDate,NSDateFormatter,NSTimeInterval
  9. 流API--初体验
  10. DBoW2应用
  11. 洛谷 P1983 车站分级
  12. [转帖]linux tree命令--显示目录的树形结构
  13. GCC编译器原理(一)------GCC 工具:addr2line、ar、as、c++filt和elfedit
  14. 在安卓上用Termux安装sqlmap
  15. SANS社区邮件情报收集【2018-12-4到2019-1-19】
  16. 为什么要用lock 【readonly】object?为什么不要lock(this)?
  17. Visual Studio 2012自动添加注释(如版权信息等)
  18. PHP发送HTTP请求的6种方法
  19. 动态调用WebService方法
  20. 居于mtk芯片安卓车机系统具体流程

热门文章

  1. P1888 三角函数
  2. javascript中常用数组方法详细讲解
  3. python爬虫:爬取医药数据库drugbank
  4. vue-router 设置默认路由
  5. BAT三家互联网公司哪家更注重用户体验?
  6. Kattis - Association for Computing Machinery
  7. Codeforces Round #493 (Div. 2) B. Cutting 前缀和优化_动归水题
  8. vue实现分页器(仿element)
  9. linux--rbtree 解惑 insert
  10. NetApp 存储的常用概念普及