资源简介
c++基础封装(线程、锁、定时器、原子操作等),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++
相关资源
- 颜色识别形状识别STM103嵌入式代码
- c++ 邮件多附件群发
- c++ 透明代理(hookproxy)
- 高性能服务器代码(50_06th_server_thre
- mfc 调用redis
- FTP客户端源码(c++)
- c++ 画图(14Qt-XPS)
- c++多边形交并差运算
- VC++基于OpenGL模拟的一个3维空间模型
- c++ 虚拟摄像头
- hook,捕获所有案件,查找所有窗口,
- C语言课设计算器
- c++ 简易贪吃蛇源码
- 高精度加法(c++代码)
- C++调用百度地图案例
- 北京化工大学计算方法(C/C++)讲义
- 基于VC++的SolidWorks二次开发SolidWorks
- c++ 模拟鼠标按键
- OFD编辑器
- Beginning C++17 From Novice to Professional
- C++ STL实现
- opencv手部轮廓识别以及轨迹识别
- 百度C++编码规范
- C++ sql2008 WebServer通讯.docx
- c++ 定时关机程序源码
- 基于VSCode和CMake实现C++开发
- c++语法查询工具
- c++ 账务系统源码
- GBT 28169-2011 嵌入式软件 C语言编码规范
- c++ 猜拳小游戏
评论
共有 条评论