⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 warthreadevent.cpp

📁 ftpserver very good sample
💻 CPP
字号:
#include "StdAfx.h"#include "WarThreadEvent.h"   // class implemented#include "WarAutoLock.h"#include "WarAutoCounter.h"#include "WarTime.h"#define AUTO_LOCK WarAutoLock my_lock(mLock);/////////////////////////////// PUBLIC /////////////////////////////////////////============================= LIFECYCLE ====================================WarThreadEvent::WarThreadEvent(bool isAutoMode)    throw(WarException){#ifdef DEBUG    mNumBlockedThreads = 0;#endif#if THREAD_PTHREAD    mIsSignaled = false;    int err = ::pthread_cond_init(&mEventHandle, NULL);    if (err)        WarThrow(WarError(WAR_THREADERR_FAIED_TO_INITIALIZE_EVENT, err), NULL);#elif defined(WIN32)    if ((mEventHandle =::CreateEvent(NULL, isAutoMode == true, FALSE, NULL))        == NULL)    {        WarThrow(WarError(WAR_THREADERR_FAIED_TO_INITIALIZE_EVENT,            ::GetLastError()), NULL);    }#else  #error "Not handled"#endif}// WarThreadEventWarThreadEvent::~WarThreadEvent(){#if THREAD_PTHREAD    ::pthread_cond_destroy(&mEventHandle);#elif defined(WIN32)    ::CloseHandle(mEventHandle);#else#error "Not handled"#endif}// ~WarThreadEvent//============================= OPERATORS ====================================//============================= OPERATIONS ===================================bool WarThreadEvent::WaitForEvent(const war_uint32_t millisecondsToWait)    throw(WarException){    bool return_val = true;#ifdef _DEBUG    WarAutoCounter<int> MyAutoCnt(mNumBlockedThreads, mLock);#endif // _DEBUG#if THREAD_PTHREAD    int err = 0;    {        AUTO_LOCK        struct timespec timeout;                if (millisecondsToWait != WAIT_INFINITE)        {            struct timeval now = WarTime().GetTimeval();            time_t seconds_to_wait = millisecondsToWait / 1000;            int milliseconds_left_to_wait = millisecondsToWait % 1000;                        timeout.tv_sec = now.tv_sec + seconds_to_wait;            timeout.tv_nsec = milliseconds_left_to_wait * 10000;        }                while (!mIsSignaled && (err != ETIMEDOUT))        {            if (millisecondsToWait != WAIT_INFINITE)                err = ::pthread_cond_timedwait(&mEventHandle,                     &mLock.mHandle, &timeout);            else                err = ::pthread_cond_wait(&mEventHandle,                     &mLock.mHandle);        }        if (mIsSignaled && (err == 0))            mIsSignaled = false;    }    if (err == ETIMEDOUT)        return false;    if (err != 0)        WarThrow(WarError(WAR_THREADERR_EVENT_WAIT_FAILED, err), NULL);#elif defined(WIN32)    DWORD status_val = ::WaitForSingleObject(mEventHandle,         millisecondsToWait);    switch(status_val)    {    case WAIT_TIMEOUT:        return_val = false;        break;    case WAIT_FAILED:        WarThrow(WarError(WAR_THREADERR_EVENT_WAIT_FAILED,             ::GetLastError()), "WAIT_FAILED");    default:        ; // No action    }#else#error "Not handled"#endif    return return_val;}void WarThreadEvent::Signal()    throw(WarException){#if THREAD_PTHREAD        AUTO_LOCK        mIsSignaled = true;    ::pthread_cond_signal(&mEventHandle);#elif defined(WIN32)        ::PulseEvent(mEventHandle);#else#error "Not handled"#endif}void WarThreadEvent::SetEvent(StateE newState){#if THREAD_PTHREAD    AUTO_LOCK#endif     switch(newState)    {    case EVENT_SIGNALED:#ifdef WIN32        ::SetEvent(mEventHandle);#else        mIsSignaled = true;        ::pthread_cond_signal(&mEventHandle);#endif        break;    case EVENT_NONSIGNALED:#ifdef WIN32        ::ResetEvent(mEventHandle);#else        mIsSignaled = false;        ::pthread_cond_signal(&mEventHandle);#endif        break;    }}//============================= ACESS      ===================================//============================= INQUIRY    ===================================/////////////////////////////// PROTECTED  ////////////////////////////////////////////////////////////////// PRIVATE    ///////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -