跨平台相当于Windows事件 [英] Cross-Platform equivalent to windows events

查看:134
本文介绍了跨平台相当于Windows事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想端口某些Windows code到Linux,最好是通过独立于平台的库(例如升压),但我不知道如何端口事件code这一点。

I'm trying to port some Windows code to Linux, ideally through platform-independent libraries (eg boost), however I'm not sure how to port this bit of event code.

code的位涉及两个线程(可以称它们为A和B)。 A想要做一些事情,只有B能,所以它给B发送一个消息,然后等待B到说对其做。在windows这看起来是这样的:

The bit of code involves two threads (lets call them A and B). A wants to do something that only B can, so it sends B a message, then waits for B to say its done. In windows this looks something like:

void foo();//thread a calls this
void bar(HANDLE evt);

void foo()
{
    HANDLE evt = CreateEvent(0,FALSE,FALSE,0);
    bCall(boost::bind(&bar, evt));
    WaitForSingleObject(evt,INFINITE);
    CloseHandle(evt);
}
void bar(HANDLE evt)
{
    doSomething();
    SetEvent(evt);
}

我看着的boost ::线程库,但它没有似乎有什么,这样做,我可以看到的是关闭了boost :: condition_variable,但现在看来,是这样的装置连同一个互斥体,这是不是这里的情况。

I looked at the boost::thread library, but it didnt seem to have anything that does this, the closes I could see was the boost::condition_variable, but it appears that is means in conjunction with a mutex, which is not the case here.

推荐答案

我觉得好,跨平台相当于win32的事件是的的boost ::条件的,所以你的code可能是这个样子:

I think a good, cross-platform equivalent to win32 events is boost::condition, so your code could look something like this:

void foo()
{
    boost::mutex mtxWait; 
    boost::condition cndSignal;

    bCall(boost::bind(&bar, mtxWait, cndSignal));

    boost::mutex::scoped_lock mtxWaitLock(mtxWait);
    cndSignal.wait(mtxWait); // you could also use cndSignal.timed_wait() here
}

void bar(boost::mutex& mtxWait, boost::condition& cndSignal)
{
    doSomething();
    cndSignal.notify_one();
}

这篇关于跨平台相当于Windows事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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