信号量的实现
#ifndef _CELL_SEMAPHORE_HPP_
#define _CELL_SEMAPHORE_HPP_
#include<chrono>
#include<thread>
#include<condition_variable>
//信号量
class CELLSemaphore
{
public:
//阻塞当前线程
void wait()
{
std::unique_lock<std::mutex> lock(_mutex);
if (--_wait < 0)
{
//阻塞等待
_cv.wait(lock, [this]()->bool{
return _wakeup > 0;
});
--_wakeup;
}
}
//唤醒当前线程
void wakeup()
{
std::lock_guard<std::mutex> lock(_mutex);
if (++_wait <= 0)
{
++_wakeup;
_cv.notify_one();
}
}
private:
//改变数据缓冲区时需要加锁
std::mutex _mutex;
//阻塞等待-条件变量
std::condition_variable _cv;
//等待计数
int _wait = 0;
//唤醒计数
int _wakeup = 0;
};
#endif // !_CELL_SEMAPHORE_HPP_
//虚假唤醒
跨平台线程实现:
#ifndef _CELL_THREAD_HPP_
#define _CELL_THREAD_HPP_
#include"CELLSemaphore.hpp"
class CELLThread
{
public:
static void Sleep(time_t dt)
{
std::chrono::milliseconds t(dt);
std::this_thread::sleep_for(t);
}
private:
typedef std::function<void(CELLThread*)> EventCall;
public:
//启动线程
void Start(
EventCall onCreate = nullptr,
EventCall onRun = nullptr,
EventCall onDestory = nullptr)
{
std::lock_guard<std::mutex> lock(_mutex);
if (!_isRun)
{
_isRun = true;
if (onCreate)
_onCreate = onCreate;
if (onRun)
_onRun = onRun;
if (onDestory)
_onDestory = onDestory;
//线程
std::thread t(std::mem_fn(&CELLThread::OnWork), this);
t.detach();
}
}
//关闭线程
void Close()
{
std::lock_guard<std::mutex> lock(_mutex);
if (_isRun)
{
_isRun = false;
_sem.wait();
}
}
//在工作函数中退出
//不需要使用信号量来阻塞等待
//如果使用会阻塞
void Exit()
{
if (_isRun)
{
std::lock_guard<std::mutex> lock(_mutex);
_isRun = false;
}
}
//线程是否启动运行状态
bool isRun()
{
return _isRun;
}
protected:
//线程的运行时的工作函数
void OnWork()
{
if (_onCreate)
_onCreate(this);
if (_onRun)
_onRun(this);
if (_onDestory)
_onDestory(this);
_sem.wakeup();
_isRun = false;
}
private:
EventCall _onCreate;
EventCall _onRun;
EventCall _onDestory;
//不同线程中改变数据时需要加锁
std::mutex _mutex;
//控制线程的终止、退出
CELLSemaphore _sem;
//线程是否启动运行中
bool _isRun = false;
};
#endif // !_CELL_THREAD_HPP_