Windows事件在Linux中使用条件变量实现
Windows Event implementation in Linux using conditional variables?
我正在尝试在Linux中实现非常简单的Windows事件。仅针对我的场景-3个线程,1个主和2个次级。每个辅助线程都通过setEvent和主线程等待1个事件。示例:
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
}
因此,要实现它,我使用条件变量。但是我的问题是 - 这是正确的方法吗?还是我做错了什么?我的实施:
// 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中 - 仅在上面描述了唯一的想法。
也许您告诉我更好的解决方法?谢谢。
upd:超时非常重要,因为其中一个线程可能无法通过setEvent()。
将其基于WaitForSingleObject
waitforsingleobject 函数检查指定对象的当前状态。如果对象的状态非信号,则调用线程进入等待状态,直到对象发出信号或超时间隔为止。
该行为和代码之间的区别在于,代码将始终在条件变量上等待,因为它不会检查谓词。这引入了pthread_condt_timewait
和pthread_cond_signal
调用之间的同步问题。
发出条件变量的一般习语是:
lock sutex设置谓词解锁静音信号条件变量
等待条件变量时:
lock sutex而(!谓词){ 等待条件变量}解锁Mutex
基于试图完成的事情,可以将单独的bool
用作每个Event
的谓词。通过引入谓词,WaitForSingleObject
只能在条件变量上等待Event
未发出信号。该代码看起来与以下几个相似:
bool SetEvent (mutex, condition)
{
pthread_mutex_lock(mutex); // lock mutex
bool& signalled = find_signal(condition); // find predicate
signalled = true; // set predicate
pthread_mutex_unlock(mutex); // unlock mutex
pthread_cond_signal(condition); // signal condition variable
}
int WaitForSingleObject(mutex, condition, timeout)
{
pthread_mutex_lock(mutex); // lock mutex
bool& signalled = find_signal(condition); // find predicate
while (!signalled)
{
pthread_cond_timedwait(condition, mutex, timeout);
}
signalled = false; // reset predicate
pthread_mutex_unlock(mutex); // unlock mutex
}
stackoverflow上已经有类似的问题:waitforsingleobject和waitgormultipleObjects等于linux
此外,您可以使用信号量:
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
上一个链接不再可用。Internet存档的Wayback Machine上最新的存档版本是:https://web.archive.org/web/201305152223326/http://www.ibm.com/develovelingerworks/linux/library/library/l-ipc2lin3/index.htex.htex.html
我认为信号量是在这里更好的解决方案,因为它可以使用间处理。您可以包装接口,如果不提供名称,请使用pthread_将其初始化以供使用中的程序中使用,这可以简短资源使用情况,但是当使用名称时,请尝试将其使用SEM初始化,以供内部过程中使用。
- 基于模板值的条件变量
- 没有超时的C++条件变量
- 在条件变量中触发错误信号的频率是多少
- 在通知提升间处理条件变量时未按住锁会导致问题
- 通知条件变量后使用互斥锁
- 滥用条件变量
- 升压插值条件变量可以虚假唤醒吗?
- 子线程中的条件变量等待停止主线程中的执行
- 条件变量基本示例
- 正在连接的等待条件变量的线程会发生什么情况?
- C++11如何在1个线程中使用条件变量处理2个线程安全队列
- 当线程处理不同的类时,应该在哪里声明条件变量、互斥对象
- 为什么在同一条件变量上使用多个互斥锁会使此代码崩溃?
- 条件变量:wait_for.gcc错误
- 如何"stop"正在等待条件变量的分离线程?
- 如何杀死被条件变量锁定的线程?
- 使用互斥锁和条件变量作为成员时如何修复"use of deleted function"?
- C++ 多个使用者线程卡在条件变量上
- POSIX 条件变量和互斥体"竞争"
- 将cpp_redis pub/sub与条件变量一起使用时出现问题