Linux/POSIX equivalent for Win32's CreateEvent

2020-02-01 06:14发布

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

Here is the Win32 implementation of my interface :

    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 :

    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()
        {

        }
    };

4条回答
走好不送
2楼-- · 2020-02-01 06:52

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

CreateEvent --> eventfd

CloseHandle --> close

SetEvent --> write

WaitForSingleObject --> read

WaitForMultipleObjects --> select and read corresponding fd

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

查看更多
Root(大扎)
3楼-- · 2020-02-01 06:55

Our open-source pevents library is an implementation of just that for all platforms. It's a very small (single-file) bit of C++ code that you can just add to your existing project and get access to the Windows event api built on top of pthreads synchronization primitives.

The most important tidbit is that it includes WaitForMultipleObjects support.

https://github.com/neosmart/pevents

查看更多
闹够了就滚
4楼-- · 2020-02-01 06:58

The pthreads equivalent of your code is:

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);
    }
};
查看更多
祖国的老花朵
5楼-- · 2020-02-01 07:08

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:

CreateEvent -> pthread_cond_init

CloseHandle -> pthread_cond_destroy

WaitForSingleObject -> pthread_cond_wait or pthread_cond_timedwait

SetEvent -> pthread_cond_signal or pthread_cond_broadcast

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

查看更多
登录 后发表回答