博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Monitor (synchronization)条件变量-安全对象
阅读量:6027 次
发布时间:2019-06-20

本文共 3250 字,大约阅读时间需要 10 分钟。

In , a monitor is a synchronization construct that allows  to have both  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  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 , , or  that uses wrapped  in order to safely allow access to a method or variable by more than one . The defining characteristic of a monitor is that its methods are executed with : At each point in time, at most one thread may be executing any of its . 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[]

Problem statement[]

For many applications, mutual exclusion is not enough. Threads attempting an operation may need to wait until some condition P holds true. A  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[]

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

 

转载地址:http://enzhx.baihongyu.com/

你可能感兴趣的文章
Software Engineering 招聘要求
查看>>
【转载】InstallAnyWhere自动化制作安装包的知识
查看>>
69、iSCSI共享存储配置实战
查看>>
文本编程
查看>>
乔布斯走了。你还期待苹果吗?
查看>>
优先级
查看>>
Tomcat与Web服务器、应用服务器的关系
查看>>
用DFS实现全排列 & 八皇后问题
查看>>
深度学习博客
查看>>
Android总结篇系列:Android Service
查看>>
Android dumpsys命令的使用
查看>>
Linux Kernel系列一:开篇和Kernel启动概要
查看>>
BZOJ 2756: [SCOI2012]奇怪的游戏 网络流/二分
查看>>
master + worker模式的node多核解决框架——node-cluster
查看>>
Android如何实现超级棒的沉浸式体验
查看>>
使用node打造自己的命令行工具方法教程
查看>>
Express代理中间件问题与解决方案
查看>>
||和&&返回什么?
查看>>
linux在文件中查找指定字符串,然后根据查找结果来做进一步的处理
查看>>
在Oracle中删除所有强制性外键约束
查看>>