Linux/POSIX equivalent for Win32's CreateEvent, SetEvent, WaitForSingleObject

后端 未结 4 2062
感情败类
感情败类 2020-12-03 20:09

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

Here is the Win32 implementation of my interface :



        
相关标签:
4条回答
  • 2020-12-03 20:24

    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

    0 讨论(0)
  • 2020-12-03 20:31

    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);
        }
    };
    
    0 讨论(0)
  • 2020-12-03 20:41

    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

    0 讨论(0)
  • 2020-12-03 20:47

    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.

    0 讨论(0)
提交回复
热议问题