網站首頁 編程語言 正文
一、前言
利用 WaitForSingleObject 檢查內核對象的狀態來進行實現。后來隨著需要開發跨平臺的程序后這種方式就不能使用了, 畢竟這是 windows 專屬。 POCO 庫在 windows之外的系統選擇利用 pthread 線程庫庫來實現 Event, pthread 畢竟不在C++標準庫里面,假設要使用的話需要單獨配置。所以本文就利用 C++11之后的標準庫內容來實現一個事件類, 方便使用。
本文代碼地址:https://github.com/pengguoqing/samples_code
二、實現
2.1 需要具備的功能
①支持多線程
②可以等待一定時間
2.2 代碼實現
一共提供四個對外接口, 分別如下:
//設置事件有信號
inline void SetEvent() const;
//重置事件為無信號
inline void ResetEvent() const;
//持續等待直到事件有信號, 如果是 Auto 模式則必須等待到該線程能拿到狀態
inline void Wait() const;
//等待事件一定時間, 時間范圍內事件有信號則返回 true,否則返回 false
template <typename Rep, typename Period>
inline bool tryWait(const std::chrono::duration<Rep, Period>& duratio) const;
類內部再對具體的實現進行一下封裝
struct EventImpl
{
explicit EventImpl(Mode mode, bool initState);
~EventImpl() = default;
inline void Set();
inline void Reset();
inline void Wait();
template <typename Rep, typename Period>
inline bool Wait_for(const std::chrono::duration<Rep, Period>& duration);
std::mutex m_mutex;
std::condition_variable m_condi;
bool m_signal;
const Mode m_mode;
};
使用 unique_ptr 對 EventImpl 資源進行管理:
const std::shared_ptr<EventImpl> m_event;
因為沒有裸露的指針資源,移動和拷貝函數就直接全部使用編譯器默認的
explicit CXEvent(Mode mode = Mode::Auto, bool initstate = false);
~CXEvent() = default;
三、測試
測試用一個事件實例來實現兩個線程的同步測試, 鍵盤輸入字符 ‘s’ 觸發一次事件, 每個線程 Wait() 兩次事件后,再測試一下 tryWait() 時間。代碼如下:
using namespace std;
static CXEvent kEvent;
static constexpr int testCnt = 3;
TestEventFunc1(const CXEvent& refEvent)
{
for (int i=0; i< testCnt; i++)
{
if (i == testCnt-1)
{
auto begin = chrono::high_resolution_clock::now();
kEvent.tryWait(5000ms);
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(end - begin);
cout << "TestEventFunc1"<<"tryWait time:"<< duration.count() << endl;
}
else
{
kEvent.Wait();
cout << "TestEventFunc1 Wait" << endl;
}
}
}
TestEventFunc2(const CXEvent& refEvent)
{
for (int i=0; i<testCnt; i++)
{
if (i == testCnt-1)
{
auto begin = chrono::high_resolution_clock::now();
kEvent.tryWait(5000ms);
auto end = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(end - begin);
cout << "TestEventFunc2" << "tryWait time:"<< duration.count()<< endl;
}
else
{
kEvent.Wait();
cout << "TestEventFunc2 Wait" << endl;
}
}
}
int main()
{
CXEvent refEvent1(kEvent);
CXEvent refEvent2(kEvent);
std::thread testTh1(TestEventFunc1, refEvent1);
std::thread testTh2(TestEventFunc2, refEvent2);
int cinCnt{0};
char inputCmd {'R'};
cin.get(inputCmd);
while ('q'!=inputCmd && cinCnt<testCnt*2)
{
if ('s' == inputCmd)
{
kEvent.SetEvent();
cinCnt++;
}
cin.get(inputCmd);
}
if (testTh1.joinable())
{
testTh1.join();
}
if (testTh2.joinable())
{
testTh2.join();
}
std::cout << "test set event and wait_for event\n";
}
期望結果是每輸入一次 ‘s’ 回車后,TestEventFunc1或者TestEventFunc2線程函數中的循環體就會被執行一次,每個循環體執行兩次后等待 5 秒線程就退出,再輸入‘q’ 退出程序。實際運行結果如下:
符合預期。
原文鏈接:https://blog.csdn.net/PX1525813502/article/details/127825325
相關推薦
- 2022-04-15 c語言?深入理解函數的遞歸_C 語言
- 2022-07-11 jenkins數據遷移和備份
- 2022-04-16 pycharm實現設置自動的參數注釋標識_python
- 2022-07-02 C語言詳細講解指針數組的用法_C 語言
- 2022-09-27 Kotlin示例講解標準函數with與run和apply的使用_Android
- 2022-07-10 CountDownLatch使用詳解
- 2023-12-08 去掉input各種邊框
- 2022-09-15 Linux添加Python?path方法及修改環境變量的三種方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支