Linux中使用条件变量的Windows事件实现? [英] Windows Event implementation in Linux using conditional variables?
问题描述
我正在尝试在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);
一切似乎都很好,但是我认为,当我在调用辅助线程中的SetEvent之前在主线程中调用WaitFor ..函数时,将出现该问题.在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().
推荐答案
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);
Detailed description and various examples could be found here: ------http://www.ibm.com/developerworks/linux/library/l-ipc2lin3/index.html
上一个链接不再可用. Internet存档的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屋!