相当于Win32的CreateEvent,SetEvent,WaitForSingleObject的Linux/POSIX [英] Linux/POSIX equivalent for Win32's CreateEvent, SetEvent, WaitForSingleObject

查看:532
本文介绍了相当于Win32的CreateEvent,SetEvent,WaitForSingleObject的Linux/POSIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个小类来同步Linux(实际上是Android)和Windows的线程.

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

这是我界面的Win32实现:

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 );
        }
    };

问题是我不确定POSIX等效什么. 到目前为止,我已经基于此SO问题<,但是由于答案不完整,所以我不确定如何完成我的课程:

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

        }
    };

推荐答案

您所描述的POSIX等效项是POSIX条件变量.请注意,条件变量必须始终与POSIX互斥锁结合使用,但是很多情况下,多个条件变量使用同一个互斥锁,因此,如果您不打算将互斥锁专门用于条件变量,则不应将其放置在班级.在您的情况下,Win32和POSIX API之间的含义对应的映射应为:

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_waitpthread_cond_timedwait

SetEvent-> pthread_cond_signalpthread_cond_broadcast

幸运的是,尽管我建议使用基本的

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

这篇关于相当于Win32的CreateEvent,SetEvent,WaitForSingleObject的Linux/POSIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆