使用简单的信号量 [英] Using simple semaphores

查看:90
本文介绍了使用简单的信号量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于一种简单的老式信号量:一个线程对其进行等待",从而被阻塞.另一个线程发出信号,从而解除阻塞等待线程,而不会更改信令线程的状态.但是在Microsoft文档中,找不到任何此类信号灯.所有信号量都与进入和离开关键部分有关.

这是我的问题:我的主线程A一直运行.线程B是一个专用线程,仅在与特定设备进行交互时才需要运行.因此,为了避免浪费CPU时间,我想阻止它,除非线程A告诉它运行.没有要保护的关键部分,如果不必要地运行,除了浪费CPU时间外,什么也不会丢失.

鉴于Microsoft信号量工具,实现我所需的最佳方法是什么?

感谢所有人,

I am used to a simple, old-fashioned form of semaphore: one thread does a Wait on it and thereby becomes blocked. Another thread does a Signal and thereby unblocks the waiting thread, without changing the state of the signaling thread.  But in the Microsoft documentation, I can't find any such semaphore. All the semaphores have to do with entering and leaving critical sections.

Here's my problem: thread A, my main thread, runs all the time. Thread B is a special purpose thread, and only needs to run when I'm interacting with a particular device. So to avoid letting it waste CPU time, I want to block it except when thread A tells it to run. There is no critical section to be protected, and nothing is lost except wasted CPU time, if it runs needlessly.

Given the Microsoft semaphore tools, what is the best way to implement what I need?

Thanks to all,

推荐答案

从.NET 2.0开始,提供了System.Threading.Semaphore.

http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

如果这不能满足您的需求,请回信解释原因.
As of .NET 2.0, there is System.Threading.Semaphore.

http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx

If this does not meet your needs, write back explaining why.


这篇关于使用简单的信号量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆