如何检查信号的状态, [英] How to check the state of a semaphore

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

问题描述

我要检查状态的旗语,看它是否获得信号,或者没有(因此,如果t的信号,我可以释放它)。我怎样才能做到这一点?

I want to check the state of a Semaphore to see if it is signalled or not (so if t is signalled, I can release it). How can I do this?

EDIT1:

我有两个线程,一会等待信号灯和其他应释放旗语。问题是,第二个线程可以调用发行()几次当第一个线程没有等待就可以了。所以第二个线程应该检测,如果它调用发行()它产生的任何错误或不(如果你试图释放一个信号,如果没有人在等待它它产生错误) 。我怎样才能做到这一点?我知道,我可以用一个标志,要做到这一点,但它是丑陋的。有没有什么更好的办法?

I have two threads, one would wait on semaphore and the other should release a Semaphore. The problem is that the second thread may call Release() several times when the first thread is not waiting on it. So the second thread should detect that if it calls Release() it generate any error or not (it generate an error if you try to release a semaphore if nobody waiting on it). How can I do this? I know that I can use a flag to do this, but it is ugly. Is there any better way?

推荐答案

您可以检查,看是否有旗语是通过调用的WaitOne <信号/ code>和传球为0的超时值作为参数。这将导致的WaitOne 来与true或false值,指示信号是否信号立即返回。当然,这可能改变,这使得它使用麻烦的信号的状态。

You can check to see if a Semaphore is signaled by calling WaitOne and passing a timeout value of 0 as a parameter. This will cause WaitOne to return immediately with a true or false value indicating whether the semaphore was signaled. This, of course, could change the state of the semaphore which makes it cumbersome to use.

另一个原因,这一招不会帮你是因为信号是说当至少一个计数可发出信号。这听起来像你想知道什么时候该信号量有可用的所有罪名。该旗语类没有,准确的能力。您可以使用从发布返回值来推断数是什么,但导致该信号改变其状态,当然,它仍然会抛出一个异常,如果信号已经让所有可用的罪名之前进行调用。

Another reason why this trick will not help you is because a semaphore is said to be signaled when at least one count is available. It sounds like you want to know when the semaphore has all counts available. The Semaphore class does not have that exact ability. You can use the return value from Release to infer what the count is, but that causes the semaphore to change its state and, of course, it will still throw an exception if the semaphore already had all counts available prior to making the call.

我们需要的是与不扔释放操作信号量。这是不是非常困难的。该 TryRelease 下面的方法将返回true,如果计数面世或假,如果信号已在 maximumCount 。无论哪种方式,它永远不会抛出异常。

What we need is a semaphore with a release operation that does not throw. This is not terribly difficult. The TryRelease method below will return true if a count became available or false if the semaphore was already at the maximumCount. Either way it will never throw an exception.

public class Semaphore
{
    private int count = 0;
    private int limit = 0;
    private object locker = new object();

    public Semaphore(int initialCount, int maximumCount)
    {
        count = initialCount;
        limit = maximumCount;
    }

    public void Wait()
    {
        lock (locker)
        {
            while (count == 0) 
            {
                Monitor.Wait(locker);
            }
            count--;
        }
    }

    public bool TryRelease()
    {
        lock (locker)
        {
            if (count < limit)
            {
                count++;
                Monitor.PulseAll(locker);
                return true;
            }
            return false;
        }
    }
}

这篇关于如何检查信号的状态,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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