Win32的CreateEvent, SetEvent, WaitForSingleObject的Linux/POSIX等价

时间:2022-01-27 20:37:06

I have written a small class for synchronizing threads of both Linux (actually Android) and Windows.

我编写了一个小类,用于同步Linux(实际上是Android)和Windows的线程。

Here is the Win32 implementation of my interface :

下面是我的接口Win32实现:

    class SyncObjectWin32 : public SyncObject
    {
    private:

        const HANDLE m_hEvent;

    public:

        SyncObjectWin32()
          : m_hEvent( ::CreateEvent( NULL, FALSE, FALSE ) )
        {
            if( NULL == m_hEvent )
                throw core::Exception( "sys::SyncObjectWin32::SyncObjectWin32() - Failed to create event." );
        }

        ~SyncObjectWin32()
        {
            ::CloseHandle( m_hEvent );
        }

        void WaitForSignal()
        {
            ::WaitForSingleObject( m_hEvent );
        }

        void Signal()
        {
            ::SetEvent( m_hEvent );
        }
    };

The problem is that i'm not sure what would be the POSIX equivalent. So far i've written the following class, based on this SO question, but since the answer is incomplete i'm not sure about how to finish my class :

问题是,我不确定POSIX是否等价。到目前为止,我已经基于这个So问题写了下面的课程,但是由于答案是不完整的,我不确定如何完成我的课程:

    class SyncObjectPosix
    {
    private:

        pthread_mutex_t m_oMutex;

    public:

        SyncObjectPosix()
        {
            pthread_mutex_lock( m_oMutex );                 // lock mutex
            bool & signalled = find_signal( condition );  // find predicate
            signalled = true;                          // set predicate
            pthread_mutex_unlock( m_oMutex );               // unlock mutex
            pthread_cond_signal( condition );            // signal condition variable
        }

        ~SyncObjectPosix()
        {

        }

        void WaitForSignal()
        {
            pthread_mutex_lock(mutex);                         // lock mutex
            bool & signalled = find_signal( condition );          // find predicate
            while (!signalled)
            {
              pthread_cond_timedwait(condition, m_oMutex, timeout);
            }
            signalled = false;                                 // reset predicate
            pthread_mutex_unlock( m_oMutex );                       // unlock mutex
        }

        void Signal()
        {

        }
    };

3 个解决方案

#1


6  

The POSIX equivalent for what you described is POSIX condition variables. Note that condition variable must always be used in pair with a POSIX mutex, but quite frequently several condition variables use the same mutex, so if you aren't going to use the mutex exclusively for the condition variable, you shouldn't place it in the class. The mappings by meaning in your case between Win32 and POSIX API should be:

正如你所描述的,POSIX等价于POSIX条件变量。注意,条件变量必须始终与POSIX互斥对象一起使用,但是经常有几个条件变量使用相同的互斥对象,所以如果您不打算将互斥对象专门用于条件变量,那么不应该将其放在类中。Win32和POSIX API之间的映射应该是:

CreateEvent -> pthread_cond_init

CreateEvent - > pthread_cond_init

CloseHandle -> pthread_cond_destroy

CloseHandle - > pthread_cond_destroy

WaitForSingleObject -> pthread_cond_wait or pthread_cond_timedwait

WaitForSingleObject -> pthread_cond_wait或pthread_cond_timedwait

SetEvent -> pthread_cond_signal or pthread_cond_broadcast

SetEvent -> pthread_cond_signal或pthread_cond_broadcast。

Fortunately, there is a lot of documentation regarding this, though I recommend the fundamental Programming POSIX Threads.

幸运的是,有很多关于这个的文档,尽管我推荐基本的编程POSIX线程。

#2


7  

Check also eventfd. It seems to be almost equivalent of CreateEvent if you need just one consumer and one producer.

检查也eventfd。如果你只需要一个消费者和一个生产者,它就几乎等同于CreateEvent。

CreateEvent --> eventfd

CreateEvent - - > eventfd

CloseHandle --> close

CloseHandle - - >关闭

SetEvent --> write

SetEvent - - >写

WaitForSingleObject --> read

WaitForSingleObject - - >阅读

WaitForMultipleObjects --> select and read corresponding fd

waittipleobjects——>选择并读取相应的fd。

Some more reading http://www.sourcexr.com/articles/2013/10/26/lightweight-inter-process-signaling-with-eventfd

更多的阅读http://www.sourcexr.com/articles/2013/10/26/lightweight-inter-process-signaling-with-eventfd

#3


2  

The pthreads equivalent of your code is:

与您的代码等价的pthreads:

class SyncObjectPosix
{
private:

    bool signalled;
    pthread_mutex_t mutex;
    pthread_cond_t cond;

public:

    SyncObjectPosix()
    {
        signalled = false;
        pthread_mutex_init(&mutex, NULL);
        pthread_cond_init(&cond, NULL);
    }

    ~SyncObjectPosix()
    {
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
    }

    void WaitForSignal()
    {
        pthread_mutex_lock(&mutex);
        while (!signalled)
        {
            pthread_cond_wait(&cond, &mutex);
        }
        signalled = false;
        pthread_mutex_unlock(&mutex);
    }

    void Signal()
    {
        pthread_mutex_lock(&mutex);
        signalled = true;
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);
    }
};

#1


6  

The POSIX equivalent for what you described is POSIX condition variables. Note that condition variable must always be used in pair with a POSIX mutex, but quite frequently several condition variables use the same mutex, so if you aren't going to use the mutex exclusively for the condition variable, you shouldn't place it in the class. The mappings by meaning in your case between Win32 and POSIX API should be:

正如你所描述的,POSIX等价于POSIX条件变量。注意,条件变量必须始终与POSIX互斥对象一起使用,但是经常有几个条件变量使用相同的互斥对象,所以如果您不打算将互斥对象专门用于条件变量,那么不应该将其放在类中。Win32和POSIX API之间的映射应该是:

CreateEvent -> pthread_cond_init

CreateEvent - > pthread_cond_init

CloseHandle -> pthread_cond_destroy

CloseHandle - > pthread_cond_destroy

WaitForSingleObject -> pthread_cond_wait or pthread_cond_timedwait

WaitForSingleObject -> pthread_cond_wait或pthread_cond_timedwait

SetEvent -> pthread_cond_signal or pthread_cond_broadcast

SetEvent -> pthread_cond_signal或pthread_cond_broadcast。

Fortunately, there is a lot of documentation regarding this, though I recommend the fundamental Programming POSIX Threads.

幸运的是,有很多关于这个的文档,尽管我推荐基本的编程POSIX线程。

#2


7  

Check also eventfd. It seems to be almost equivalent of CreateEvent if you need just one consumer and one producer.

检查也eventfd。如果你只需要一个消费者和一个生产者,它就几乎等同于CreateEvent。

CreateEvent --> eventfd

CreateEvent - - > eventfd

CloseHandle --> close

CloseHandle - - >关闭

SetEvent --> write

SetEvent - - >写

WaitForSingleObject --> read

WaitForSingleObject - - >阅读

WaitForMultipleObjects --> select and read corresponding fd

waittipleobjects——>选择并读取相应的fd。

Some more reading http://www.sourcexr.com/articles/2013/10/26/lightweight-inter-process-signaling-with-eventfd

更多的阅读http://www.sourcexr.com/articles/2013/10/26/lightweight-inter-process-signaling-with-eventfd

#3


2  

The pthreads equivalent of your code is:

与您的代码等价的pthreads:

class SyncObjectPosix
{
private:

    bool signalled;
    pthread_mutex_t mutex;
    pthread_cond_t cond;

public:

    SyncObjectPosix()
    {
        signalled = false;
        pthread_mutex_init(&mutex, NULL);
        pthread_cond_init(&cond, NULL);
    }

    ~SyncObjectPosix()
    {
        pthread_mutex_destroy(&mutex);
        pthread_cond_destroy(&cond);
    }

    void WaitForSignal()
    {
        pthread_mutex_lock(&mutex);
        while (!signalled)
        {
            pthread_cond_wait(&cond, &mutex);
        }
        signalled = false;
        pthread_mutex_unlock(&mutex);
    }

    void Signal()
    {
        pthread_mutex_lock(&mutex);
        signalled = true;
        pthread_mutex_unlock(&mutex);
        pthread_cond_signal(&cond);
    }
};