使用简单的信号量

编程入门 行业动态 更新时间:2024-10-24 16:30:31
本文介绍了使用简单的信号量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我习惯于一种简单的老式信号量:一个线程对其进行等待",从而被阻塞.另一个线程发出信号,从而解除阻塞等待线程,而不会更改信令线程的状态.但是在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. msdn.microsoft/en-us/library/system.threading.semaphore.aspx 如果这不能满足您的需求,请回信解释原因. As of .NET 2.0, there is System.Threading.Semaphore.msdn.microsoft/en-us/library/system.threading.semaphore.aspxIf this does not meet your needs, write back explaining why.

更多推荐

使用简单的信号量

本文发布于:2023-10-09 06:22:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信号量   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!