資源簡介
c++基礎(chǔ)封裝(線程、鎖、定時器、原子操作等),c++封裝,接口方便好用。

代碼片段和文件信息
/**
?*?Copyright?(C)?2013?kangliqiang?kangliq@163.com
?*
?*?Licensed?under?the?Apache?License?Version?2.0?(the?“License“);
?*?you?may?not?use?this?file?except?in?compliance?with?the?License.
?*?You?may?obtain?a?copy?of?the?License?at
?*
?*?????http://www.apache.org/licenses/LICENSE-2.0
?*
?*?Unless?required?by?applicable?law?or?agreed?to?in?writing?software
?*?distributed?under?the?License?is?distributed?on?an?“AS?IS“?BASIS
?*?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND?either?express?or?implied.
?*?See?the?License?for?the?specific?language?governing?permissions?and
?*?limitations?under?the?License.
?*/
#include?“Condition.h“
#include?
#include?
#include?“Mutex.h“
#include?“ScopedLock.h“
#include?“Semaphore.h“
#include?“KPRUtil.h“
#include?“Exception.h“
namespace?kpr
{
#if?defined(WIN32)
class?CondImpl
{
public:
CondImpl()
:?m_gate(1)
??m_blocked(0)
??m_unblocked(0)
??m_toUnblock(0)
{
}
void?Notify(bool?broadcast)
{
m_gate.Wait();
m_internal.Lock();
if?(m_unblocked?!=?0)
{
m_blocked?-=?m_unblocked;
m_unblocked?=?0;
}
if?(m_blocked?>?0)
{
m_toUnblock?=?(broadcast)???m_blocked?:?1;
m_internal.Unlock();
m_queue.Release();
}
else
{
m_gate.Release();
m_internal.Unlock();
}
}
void?PreWait()
{
m_gate.Wait();
++m_blocked;
m_gate.Release();
}
bool?Wait(long?timeout)
{
try
{
bool?rc?=?m_queue.Wait(timeout);
postwait(!rc);
return?rc;
}
catch(...)
{
postwait(false);
throw;
}
}
private:
void?postwait(bool?timeout)
{
m_internal.Lock();
++m_unblocked;
if?(m_toUnblock?!=?0)
{
bool?last?=?(--m_toUnblock?==?0);
m_internal.Unlock();
if?(last)
{
m_gate.Release();
}
else
{
m_queue.Release();
}
}
else
{
m_internal.Unlock();
}
}
Semaphore?m_gate;
Semaphore?m_queue;
Mutex?m_internal;
long?m_blocked;
long?m_unblocked;
long?m_toUnblock;
};
#else
class?Conditionhelper
{
RecursiveMutex&?m_mutex;
int?m_count;
public:
Conditionhelper(RecursiveMutex&?mutex?int?count)
:?m_mutex(mutex)
m_count(count)
{
}
~Conditionhelper()
{
pthread_mutex_unlock(&m_mutex.m_mutex);
m_mutex.lock(m_count);
}
};
#endif
Condition::Condition()
{
#ifdef?WIN32
m_impl?=?new?CondImpl;
#else
pthread_cond_init(&m_cond?0);
#endif
}
Condition::~Condition()
{
#if?defined(WIN32)
delete?m_impl;
#else
pthread_cond_destroy(&m_cond);
#endif
}
void?Condition::Wait(Mutex&?mutex)
{
wait(mutex?-1);
}
bool?Condition::Wait(Mutex&?mutex?long?timeout)
{
assert(timeout>=0&&“timeout?value?is?negative“);
return?wait(mutex?timeout);
}
void?Condition::Wait(?RecursiveMutex&?mutex?)
{
wait(mutex?-1);
}
bool?Condition::Wait(?RecursiveMutex&?mutex?long?timeout?)
{
assert(timeout>=0&&“timeout?value?is?negative“);
return?wait(mutex?timeout);
}
void?Condition::Notify()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.......??????4575??2017-04-01?16:23??kpr\AtomicValue.h
????.......???????840??2017-11-08?16:03??kpr\Auto_ptr.h
????.......??????4816??2017-04-01?16:23??kpr\Condition.cpp
????.......??????1291??2017-04-01?16:23??kpr\Condition.h
????.......??????1311??2017-03-25?14:56??kpr\Epoller.cpp
????.......??????2553??2017-03-25?14:56??kpr\Epoller.h
????.......??????2096??2017-04-01?16:23??kpr\Exception.h
????.......??????1696??2017-04-01?16:23??kpr\KPRTypes.h
????.......??????1554??2017-04-01?16:23??kpr\KPRUtil.cpp
????.......??????1025??2017-04-01?16:23??kpr\KPRUtil.h
????.......??????2057??2017-04-01?16:23??kpr\Monitor.cpp
????.......??????1138??2017-04-01?16:23??kpr\Monitor.h
????.......??????5433??2017-04-01?16:23??kpr\Mutex.cpp
????.......??????2222??2017-04-01?16:23??kpr\Mutex.h
????.......???????934??2017-11-08?15:38??kpr\RefCount.cpp
????.......???????887??2017-11-08?15:38??kpr\RefCount.h
????.......??????2132??2017-11-08?15:38??kpr\RefHandle.h
????.......??????1336??2017-04-01?16:23??kpr\ScopedLock.h
????.......??????1751??2017-04-01?16:23??kpr\Semaphore.cpp
????.......???????970??2017-04-01?16:23??kpr\Semaphore.h
????.......??????3842??2017-11-08?15:38??kpr\Thread.cpp
????.......??????1617??2017-04-01?16:23??kpr\Thread.h
????.......??????1478??2017-04-01?16:23??kpr\ThreadLocal.cpp
????.......???????887??2017-04-01?16:23??kpr\ThreadLocal.h
????.......??????7392??2017-04-01?16:23??kpr\ThreadPool.cpp
????.......??????2723??2017-04-01?16:23??kpr\ThreadPool.h
????.......???????760??2017-04-01?16:23??kpr\ThreadPoolWork.h
????.......??????2208??2017-04-01?16:23??kpr\TimerTaskManager.cpp
????.......??????1788??2017-04-01?16:23??kpr\TimerTaskManager.h
????.......??????3860??2017-04-01?16:23??kpr\TimerThread.cpp
............此處省略5個文件信息
- 上一篇:MD5 碰撞生成器 C++源碼
- 下一篇:adaboost算法源碼c++
評論
共有 條評論