c++ 加入信號量控制線程的終止和退出

信號量的實現

#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_

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章