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

后端 未结 4 2075
感情败类
感情败类 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: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);
        }
    };
    

提交回复
热议问题