ManualResetEvent的提升等同? [英] Boost equivalent of ManualResetEvent?

查看:92
本文介绍了ManualResetEvent的提升等同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有一个升压相当于ManualResetEvent的呢?基本上,我想一个跨平台实现...或者,可能有人帮助我使用boost ::线程模仿的ManualResetEvent的功能?谢谢你们

I'm wondering if there is a boost equivalent of ManualResetEvent? Basically, I'd like a cross-platform implementation... Or, could someone help me mimic ManualResetEvent's functionality using Boost::thread? Thanks guys

推荐答案

这是pretty容易当你有互斥体和条件变量写一个手动重置事件。

It's pretty easy to write a manual reset event when you have mutexes and condition variables.

你所需要的是重新presents的复位事件是否标志着与否的字段。进入该领域将需要由一个互斥看守 - 这包括设置/重置您的活动以及检查,看它是否暗示

What you will need is a field that represents whether your reset event is signalled or not. Access to the field will need to be guarded by a mutex - this includes both setting/resetting your event as well as checking to see if it is signaled.

当你在你的事件等待,如果当前没有信号,你将要在一个条件变量等待,直到它发出信号。最后,在code,设置时,你会想通知的条件变量唤醒任何人等待您的活动。

When you are waiting on your event, if it is currently not signaled, you will want to wait on a condition variable until it is signaled. Finally, in your code that sets the event, you will want to notify the condition variable to wake up anyone waiting on your event.

class manual_reset_event
{
public:
    manual_reset_event(bool signaled = false)
        : signaled_(signaled)
    {
    }

    void set()
    {
        {
            boost::lock_guard<boost::mutex> lock(m_);
            signaled_ = true;
        }

        // Notify all because until the event is manually
        // reset, all waiters should be able to see event signalling
        cv_.notify_all();
    }

    void unset()
    {
        boost::lock_guard<boost::mutex> lock(m_);
        signaled_ = false;
    }


    void wait()
    {
        boost::lock_guard<boost::mutex> lock(m_);
        while (!signaled_)
        {
            cv_.wait(lock);
        }
    }

private:
    boost::mutex m_;
    boost::condition_variable cv_;
    bool signaled_;
};

这篇关于ManualResetEvent的提升等同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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