Windows事件在Linux中使用条件变量实现

Windows Event implementation in Linux using conditional variables?

本文关键字:条件 变量 实现 事件 Linux Windows      更新时间:2023-10-16

我正在尝试在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_timewaitpthread_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初始化,以供内部过程中使用。