使用条件变量在 Linux 中实现 Windows 事件? [英] Windows Event implementation in Linux using conditional variables?

查看:15
本文介绍了使用条件变量在 Linux 中实现 Windows 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Linux 中实现非常简单的 Windows 事件.仅适用于我的场景 - 3 个线程,1 个主线程和 2 个辅助线程.每个辅助线程通过 SetEvent 引发 1 个事件,主线程等待它.示例:

I am trying to implement very simple Windows events in Linux. Only for my scenario - 3 threads, 1 main and 2 secondary. Each of secondary threads raise 1 event by SetEvent and main thread wait it. Example:

int main()
{
    void* Events[2];
    Events[0] = CreateEvent();
    Events[1] = CreateEvent();

    pthread_start(Thread, Events[0]);
    pthread_start(Thread, Events[1]);

    WaitForMultipleObjects(2, Events, 30000) // 30 seconds timeout

    return 0;
}

int* thread(void* Event)
{
    // Do something
    SetEvent(Event);
    // Do something
}

所以,为了实现它,我使用条件变量.但我的问题是 - 这是正确的方法吗?还是我做错了什么?我的实现:

So, to implement it, i use conditional variables. But my question is - is this a right way? Or i doing something wrong? My implementation:

// Actually, this function return pointer to struct with mutex and cond
// here i just simplified example
void* CreateEvent(mutex, condition)
{
    pthread_mutex_init(mutex, NULL);
    pthread_cond_init(condition, NULL);
}

bool SetEvent (mutex, condition)
{
    pthread_mutex_lock(mutex);
    pthread_cond_signal(condition);
    pthread_mutex_unlock(mutex);
}

int WaitForSingleObject(mutex, condition, timeout)
{
    pthread_mutex_lock(mutex);
    pthread_cond_timedwait(condition, mutex, timeout);
    pthread_mutex_unlock(mutex);
}

// Call WaitForSingleObject for each event. 
// Yes, i know, that its a wrong way, but it should work in my example.
int WaitForMultipleObjects(count, mutex[], condition[], timeout);

一切似乎都很好,但我认为,当我在主线程中调用 WaitFor.. 函数时,会出现该问题,然后调用辅助线程中的 SetEvent.在 Windows 中,它运行良好,但在 Linux 中 - 上面仅描述了想法.

And all seems good, but i think, that problem will appear when i call WaitFor.. function in Main thread before SetEvent in secondary thread will be called. In Windows, it worked well, but in Linux - only idea is described above.

也许你能告诉我更好的解决方法?谢谢.

Maybe you tell me the better way to solve it? Thank you.

UPD:超时非常重要,因为其中一个辅助线程可能不会通过 SetEvent().

UPD: Timeout is very important, because one of the secondary threads may not pass SetEvent().

推荐答案

stackoverflow 上已经有类似问题:WaitForSingleObject 和 WaitForMultipleObjects 在 linux 中的等价物

There was similar question on stackoverflow already: WaitForSingleObject and WaitForMultipleObjects equivalent in linux

另外你可以使用信号量:

In addition you can use semaphores:

sem_t semOne  ;
sem_t semTwo  ;
sem_t semMain ;

在主线程中:

sem_init(semOne,0,0) ;
sem_init(semTwo,0,0) ;
sem_init(semMain,0,0) ;

...


sem_wait(&semMain);

// Thread 1
sem_wait(&semOne);
sem_post(&semMain);


// Thread 2
sem_wait(&semTwo);
sem_post(&semMain);

详细描述和各种示例可以在这里找到:------http://www.ibm.com/developerworks/linux/library/l-ipc2lin3/index.html

Detailed description and various examples could be found here: ------http://www.ibm.com/developerworks/linux/library/l-ipc2lin3/index.html

以前的链接不再可用.The Internet Archive's Wayback Machine 的最新存档版本是:https://web.archive.org/web/20130515223326/http://www.ibm.com/developerworks/linux/library/l-ipc2lin3/index.html

The previous link is no longer available. The most recent archived version at The Internet Archive's Wayback Machine is: https://web.archive.org/web/20130515223326/http://www.ibm.com/developerworks/linux/library/l-ipc2lin3/index.html

这篇关于使用条件变量在 Linux 中实现 Windows 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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