• 信号量的实现


    class OSCond
    {
    public:

        OSCond();
        ~OSCond();

        inline void     Signal();
        inline void     Wait(OSMutex* inMutex, SInt32 inTimeoutInMilSecs = 0);
        inline void     Broadcast();

    private:

    #ifdef __Win32__
        HANDLE              fCondition;
        UInt32              fWaitCount;
    #elif __PTHREADS_MUTEXES__
        pthread_cond_t      fCondition;
        void                TimedWait(OSMutex* inMutex, SInt32 inTimeoutInMilSecs);
    #else
        mycondition_t       fCondition;
    #endif
    };

    inline void OSCond::Wait(OSMutex* inMutex, SInt32 inTimeoutInMilSecs)
    {
    #ifdef __Win32__
        DWORD theTimeout = INFINITE;
        if (inTimeoutInMilSecs > 0)
            theTimeout = inTimeoutInMilSecs;
        inMutex->Unlock();
        fWaitCount++;
        DWORD theErr = ::WaitForSingleObject(fCondition, theTimeout);
        fWaitCount--;
        Assert((theErr == WAIT_OBJECT_0) || (theErr == WAIT_TIMEOUT));
        inMutex->Lock();
    #elif __PTHREADS_MUTEXES__
        this->TimedWait(inMutex, inTimeoutInMilSecs);
    #else
        Assert(fCondition != NULL);
        mycondition_wait(fCondition, inMutex->fMutex, inTimeoutInMilSecs);
    #endif
    }

    inline void OSCond::Signal()
    {
    #ifdef __Win32__
        BOOL theErr = ::SetEvent(fCondition);
        Assert(theErr == TRUE);
    #elif __PTHREADS_MUTEXES__
        pthread_cond_signal(&fCondition);
    #else
        Assert(fCondition != NULL);
        mycondition_signal(fCondition);
    #endif
    }

    inline void OSCond::Broadcast()
    {
    #ifdef __Win32__
        //
        // There doesn't seem like any more elegant way to
        // implement Broadcast using events in Win32.
        // This will work, it may generate spurious wakeups,
        // but condition variables are allowed to generate
        // spurious wakeups
        UInt32 waitCount = fWaitCount;
        for (UInt32 x = 0; x < waitCount; x++)
        {
            BOOL theErr = ::SetEvent(fCondition);
            Assert(theErr == TRUE);
        }
    #elif __PTHREADS_MUTEXES__
        pthread_cond_broadcast(&fCondition);
    #else
        Assert(fCondition != NULL);
        mycondition_broadcast(fCondition);
    #endif
    }

    #endif //_OSCOND_H_

  • 相关阅读:
    poj 2418 Hardwood Species
    hdu 3791 二叉搜索树
    九度oj 1544 数字序列区间最小值
    九度oj 1525 子串逆序打印
    九度oj 1530 最长不重复子串
    九度oj 1523 从上往下打印二叉树
    P1190 接水问题
    P1179 数字统计
    P1083 借教室
    P1079 Vigenère 密码
  • 原文地址:https://www.cnblogs.com/cnhk19/p/13162641.html
Copyright © 2020-2023  润新知