CcosThread.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. #include "CcosThread.h"
  2. #include "MsgQueue.h"
  3. #include <iostream>
  4. #include <sys/time.h>
  5. #include <errno.h>
  6. #include <ctime>
  7. #include <thread>
  8. #include <stdexcept>
  9. #include <sstream>
  10. // 线程安全日志锁
  11. static std::mutex g_logMutex;
  12. // #define LOG(...) { std::lock_guard<std::mutex> lock(g_logMutex); std::cout << __VA_ARGS__ << std::endl; }
  13. #define ERR_LOG(...) { std::lock_guard<std::mutex> lock(g_logMutex); std::cerr << __VA_ARGS__ << std::endl; }
  14. // Thread_Base 实现
  15. Thread_Base::Thread_Base(void)
  16. : m_Base_Thread(0),
  17. m_ThreadID(0),
  18. m_ThreadRunning(false),
  19. m_ExitRequested(false),
  20. m_strName(""),
  21. m_pThreadLogger(nullptr)
  22. {
  23. //LOG("Creating exit/work/run events");
  24. m_ExitFlag = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET, false);
  25. m_WorkFlag = LinuxEvent::CreateEvent(LinuxEvent::AUTO_RESET, false);
  26. m_RunFlag = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET, false);
  27. if (!m_ExitFlag || !m_WorkFlag || !m_RunFlag) {
  28. throw std::runtime_error("Failed to create event objects");
  29. }
  30. // 初始化条件变量
  31. if (pthread_cond_init(&m_StateCond, nullptr) != 0) {
  32. throw std::runtime_error("Failed to initialize condition variable");
  33. }
  34. }
  35. Thread_Base::~Thread_Base(void)
  36. {
  37. try{
  38. // Stop thread before destroying, increase timeout to avoid crash
  39. if (m_ThreadRunning) {
  40. bool stopped = StopThread(10000); // 10 seconds timeout
  41. // Force wait if thread still running to prevent accessing freed memory
  42. if (!stopped && m_Base_Thread != 0) {
  43. ERR_LOG("[Thread] Force waiting for thread " << m_strName << " to exit...");
  44. pthread_join(m_Base_Thread, nullptr);
  45. m_Base_Thread = 0;
  46. m_ThreadRunning = false;
  47. }
  48. }
  49. }
  50. catch (const std::exception& e) {
  51. ERR_LOG("[Thread] Exception in ~Thread_Base: " << e.what());
  52. }
  53. catch (...) {
  54. ERR_LOG("[Thread] Unknown exception in ~Thread_Base");
  55. }
  56. pthread_cond_destroy(&m_StateCond);
  57. }
  58. void Thread_Base::SetLogger(PVOID pLoger) {
  59. m_pThreadLogger = pLoger;
  60. }
  61. PVOID Thread_Base::GetLogger() {
  62. return m_pThreadLogger;
  63. }
  64. void* Thread_Base::Thread_Base_Thread(void* pPara) {
  65. Thread_Base* handle = static_cast<Thread_Base*>(pPara);
  66. if (!handle) {
  67. ERR_LOG("Invalid thread parameter");
  68. return nullptr;
  69. }
  70. try {
  71. usleep(30000); // Sleep(30) 替代
  72. if (!handle->RegistThread()) {
  73. goto endwork_entry;
  74. }
  75. if (!handle->OnStartThread()) {
  76. goto endwork_entry;
  77. }
  78. // LOG("Thread " << handle->m_strName << " starting");
  79. handle->SetThreadOnTheRun(true);
  80. while (true) {
  81. if (!handle->Exec()) {
  82. break;
  83. }
  84. int waitevent = handle->WaitTheIncommingEvent(0);
  85. if (waitevent == 0) {
  86. break;
  87. }
  88. }
  89. endwork_entry:
  90. // LOG("Thread " << handle->m_strName << " exiting");
  91. try {
  92. handle->ThreadExitProcedure();
  93. }
  94. catch (const std::exception& e) {
  95. ERR_LOG("[Thread] ThreadExitProcedure exception in " << handle->m_strName << ": " << e.what());
  96. }
  97. catch (...) {
  98. ERR_LOG("[Thread] Unknown ThreadExitProcedure exception in " << handle->m_strName);
  99. }
  100. return nullptr;
  101. }
  102. catch (const std::exception& e) {
  103. ERR_LOG("[Thread] Exception in thread " << handle->m_strName << ": " << e.what());
  104. // 异常情况下也要执行清理
  105. try {
  106. handle->ThreadExitProcedure();
  107. }
  108. catch (...) {
  109. ERR_LOG("[Thread] Cleanup failed after exception in " << handle->m_strName);
  110. }
  111. return nullptr;
  112. }
  113. catch (...) {
  114. ERR_LOG("[Thread] Unknown exception in thread " << handle->m_strName);
  115. // 异常情况下也要执行清理
  116. try {
  117. handle->ThreadExitProcedure();
  118. }
  119. catch (...) {
  120. ERR_LOG("[Thread] Cleanup failed after unknown exception in " << handle->m_strName);
  121. }
  122. return nullptr;
  123. }
  124. }
  125. bool Thread_Base::StartThread(bool Sync, bool Inherit)
  126. {
  127. if (m_strName.empty()) {
  128. // LOG("Warning: Thread name is empty");
  129. }
  130. // 检查线程是否已运行
  131. if (m_ThreadRunning) {
  132. #ifdef __GNU_LIBRARY__ // 仅GNU环境支持非标准函数
  133. int tryJoin = pthread_tryjoin_np(m_Base_Thread, nullptr);
  134. if (tryJoin == EBUSY) {
  135. // LOG("Thread " << m_strName << " is already running");
  136. return true;
  137. }
  138. #endif
  139. // 线程已结束,清理资源
  140. // LOG("Thread " << m_strName << " has ended, cleaning up");
  141. pthread_join(m_Base_Thread, nullptr);
  142. m_ThreadRunning = false;
  143. }
  144. m_ExitFlag->ResetEvent();
  145. // 初始化线程属性
  146. pthread_attr_t attr;
  147. if (pthread_attr_init(&attr) != 0) {
  148. ERR_LOG("Failed to initialize thread attributes");
  149. return false;
  150. }
  151. // 设置栈大小
  152. size_t stack_size = 2 * 1024 * 1024; // 2MB
  153. if (pthread_attr_setstacksize(&attr, stack_size) != 0) {
  154. ERR_LOG("Failed to set thread stack size");
  155. pthread_attr_destroy(&attr);
  156. return false;
  157. }
  158. // 配置调度继承
  159. if (Inherit) {
  160. pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
  161. }
  162. else {
  163. pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
  164. }
  165. // 创建线程
  166. // LOG("Creating thread " << m_strName);
  167. int result = pthread_create(&m_Base_Thread, &attr, Thread_Base_Thread, this);
  168. pthread_attr_destroy(&attr);
  169. if (result != 0) {
  170. ERR_LOG("Failed to create thread " << m_strName << ", error: " << result);
  171. m_ExitFlag->SetEvent();
  172. return false;
  173. }
  174. m_ThreadRunning = true;
  175. m_ThreadID = static_cast<unsigned long>(m_Base_Thread);
  176. // LOG("Thread " << m_strName << " created, ID: " << m_ThreadID);
  177. // 同步等待线程启动
  178. if (Sync) {
  179. // LOG("Waiting for thread " << m_strName << " to start");
  180. return WaitTheThreadOnTheRun(300000); // 300秒超时
  181. }
  182. return true;
  183. }
  184. bool Thread_Base::StopThread(unsigned long timeperiod)
  185. {
  186. // 如果已经停止,直接返回
  187. if (!m_ThreadRunning) {
  188. return true;
  189. }
  190. bool ret = true;
  191. // 设置退出标志 - 通过信号量通知
  192. m_ExitFlag->SetEvent();
  193. // 检查是否在同一个线程内调用
  194. if (pthread_equal(pthread_self(), m_Base_Thread)) {
  195. return true;
  196. }
  197. if (m_Base_Thread) {
  198. struct timespec ts;
  199. // 计算绝对超时时间
  200. if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
  201. perror("clock_gettime");
  202. return false;
  203. }
  204. // 将毫秒转换为秒和纳秒
  205. ts.tv_sec += timeperiod / 1000;
  206. ts.tv_nsec += (timeperiod % 1000) * 1000000;
  207. // 处理纳秒溢出
  208. if (ts.tv_nsec >= 1000000000) {
  209. ts.tv_sec += 1;
  210. ts.tv_nsec -= 1000000000;
  211. }
  212. #ifdef __GNU_LIBRARY__
  213. int joinResult = pthread_timedjoin_np(m_Base_Thread, nullptr, &ts);
  214. #else
  215. // 标准环境下使用循环等待
  216. int joinResult = ETIMEDOUT;
  217. const int interval = 100; // 100ms轮询
  218. for (unsigned long elapsed = 0; elapsed < timeperiod; elapsed += interval) {
  219. int tryJoin = pthread_tryjoin_np(m_Base_Thread, nullptr);
  220. if (tryJoin == 0) {
  221. joinResult = 0;
  222. break;
  223. }
  224. usleep(interval * 1000);
  225. }
  226. #endif
  227. if (joinResult == ETIMEDOUT) {
  228. ERR_LOG("Timeout stopping thread " << m_strName << ", escalating to blocking join to avoid detach/use-after-free");
  229. // 尝试阻塞等待,确保线程退出后再清理,避免悬挂指针
  230. int forceJoin = pthread_join(m_Base_Thread, nullptr);
  231. if (forceJoin != 0) {
  232. ERR_LOG("Blocking join failed for thread " << m_strName << ", error: " << forceJoin);
  233. ret = false;
  234. } else {
  235. m_Base_Thread = 0;
  236. m_ThreadID = 0;
  237. m_ThreadRunning = false;
  238. }
  239. }
  240. else if (joinResult != 0) {
  241. ERR_LOG("Failed to join thread " << m_strName << ", error: " << joinResult);
  242. ret = false;
  243. }
  244. else {
  245. // 正常停止
  246. m_Base_Thread = 0;
  247. m_ThreadID = 0;
  248. m_ThreadRunning = false;
  249. }
  250. }
  251. return ret;
  252. }
  253. void Thread_Base::NotifyExit()
  254. {
  255. // 发送退出信号
  256. m_ExitFlag->SetEvent();
  257. }
  258. int Thread_Base::WaitTheIncommingEvent(unsigned long waittime) // 修改为 unsigned long 以匹配毫秒超时
  259. {
  260. std::vector<std::shared_ptr<LinuxEvent>> waitEvents = {
  261. m_ExitFlag,
  262. m_WorkFlag
  263. };
  264. if (m_Base_Thread)
  265. {
  266. // 等待两个事件中的一个发生
  267. int wait = LinuxEvent::WaitForMultipleEvents(waitEvents, waittime);
  268. if (wait == 0)
  269. {
  270. return 0; // ExitFlag触发
  271. }
  272. else if (wait == 1)
  273. {
  274. return 1; // WorkFlag触发
  275. }
  276. else
  277. {
  278. // 超时或错误
  279. return -1;
  280. }
  281. }
  282. // 无线程对象,默认认为退出
  283. return 0;
  284. }
  285. bool Thread_Base::WaitTheThreadEnd(unsigned long waittime)
  286. {
  287. if (m_Base_Thread == 0) {
  288. return true; // 没有线程存在,直接返回成功
  289. }
  290. // 计算绝对超时时间
  291. struct timespec ts;
  292. if (clock_gettime(CLOCK_REALTIME, &ts) == -1) {
  293. perror("clock_gettime");
  294. return false;
  295. }
  296. ts.tv_sec += waittime / 1000;
  297. ts.tv_nsec += (waittime % 1000) * 1000000;
  298. if (ts.tv_nsec >= 1000000000) {
  299. ts.tv_sec += 1;
  300. ts.tv_nsec -= 1000000000;
  301. }
  302. #ifdef __GNU_LIBRARY__
  303. int result = pthread_timedjoin_np(m_Base_Thread, nullptr, &ts);
  304. #else
  305. int result = ETIMEDOUT;
  306. const int interval = 100;
  307. for (unsigned long elapsed = 0; elapsed < waittime; elapsed += interval) {
  308. if (pthread_tryjoin_np(m_Base_Thread, nullptr) == 0) {
  309. result = 0;
  310. break;
  311. }
  312. usleep(interval * 1000);
  313. }
  314. #endif
  315. return result == 0;
  316. }
  317. bool Thread_Base::WaitTheThreadEndSign(unsigned long waittime)
  318. {
  319. return m_Base_Thread ? m_ExitFlag->Wait(waittime) : true;
  320. }
  321. bool Thread_Base::SetThreadOnTheRun(bool OnTheRun)
  322. {
  323. if (!m_ThreadRunning || !m_RunFlag) {
  324. return false;
  325. }
  326. try {
  327. if (OnTheRun) {
  328. m_RunFlag->SetEvent();
  329. }
  330. else {
  331. m_RunFlag->ResetEvent();
  332. }
  333. return true;
  334. }
  335. catch (...) {
  336. // 捕获所有异常,防止崩溃传播
  337. return false;
  338. }
  339. }
  340. void Thread_Base::NotifyThreadWork()
  341. {
  342. m_WorkFlag->SetEvent();
  343. // 发送退出信号
  344. }
  345. bool Thread_Base::WaitTheThreadOnTheRun(unsigned long waittime)
  346. {
  347. //LOG("Waiting for thread " << m_strName << " to run");
  348. if (m_Base_Thread == 0) {
  349. ERR_LOG("Thread " << m_strName << " does not exist");
  350. return false;
  351. }
  352. return m_RunFlag->Wait(waittime);
  353. }
  354. // 虚函数默认实现
  355. bool Thread_Base::Exec() { return true; }
  356. bool Thread_Base::OnStartThread() { return true; }
  357. bool Thread_Base::OnEndThread() { return true; }
  358. bool Thread_Base::RegistThread() { return true; }
  359. bool Thread_Base::UnRegistThread() { return true; }
  360. pthread_t Thread_Base::GetTID() { return m_Base_Thread; }
  361. void Thread_Base::SetName(const char* pszThreadName) { if (pszThreadName) m_strName = pszThreadName; }
  362. std::shared_ptr<LinuxEvent> Thread_Base::GetWorkEvt()
  363. {
  364. return m_WorkFlag;
  365. }
  366. std::shared_ptr<LinuxEvent> Thread_Base::GetExitEvt()
  367. {
  368. return m_ExitFlag;
  369. }
  370. void Thread_Base::ThreadExitProcedure() {
  371. // 避免重复清理
  372. if (!m_ThreadRunning) {
  373. return;
  374. }
  375. try {
  376. m_ThreadRunning = false;
  377. // 安全设置事件
  378. if (m_ExitFlag) {
  379. try {
  380. m_ExitFlag->SetEvent();
  381. }
  382. catch (const std::exception& e) {
  383. ERR_LOG("[Thread] SetEvent exception in " << m_strName << ": " << e.what());
  384. }
  385. catch (...) {
  386. ERR_LOG("[Thread] Unknown SetEvent exception in " << m_strName);
  387. }
  388. }
  389. // 保护 OnEndThread - 允许失败但不影响清理
  390. try {
  391. OnEndThread();
  392. }
  393. catch (const std::exception& e) {
  394. ERR_LOG("[Thread] OnEndThread exception in " << m_strName << ": " << e.what());
  395. }
  396. catch (...) {
  397. ERR_LOG("[Thread] Unknown OnEndThread exception in " << m_strName);
  398. }
  399. // 保护 UnRegistThread - 允许失败但不影响清理
  400. try {
  401. UnRegistThread();
  402. }
  403. catch (const std::exception& e) {
  404. ERR_LOG("[Thread] UnRegistThread exception in " << m_strName << ": " << e.what());
  405. }
  406. catch (...) {
  407. ERR_LOG("[Thread] Unknown UnRegistThread exception in " << m_strName);
  408. }
  409. // 安全设置运行状态
  410. try {
  411. SetThreadOnTheRun(false);
  412. }
  413. catch (const std::exception& e) {
  414. ERR_LOG("[Thread] SetThreadOnTheRun exception in " << m_strName << ": " << e.what());
  415. }
  416. catch (...) {
  417. ERR_LOG("[Thread] Unknown SetThreadOnTheRun exception in " << m_strName);
  418. }
  419. }
  420. catch (const std::exception& e) {
  421. ERR_LOG("[Thread] Critical ThreadExitProcedure exception in " << m_strName << ": " << e.what());
  422. }
  423. catch (...) {
  424. ERR_LOG("[Thread] Critical unknown ThreadExitProcedure exception in " << m_strName);
  425. }
  426. }
  427. // Work_Thread 实现
  428. Work_Thread::Work_Thread() {
  429. m_PauseEvt = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET, false);
  430. m_ResumeEvt = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET, false);
  431. m_SleepingEvt = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET, false);
  432. m_WorkingEvt = LinuxEvent::CreateEvent(LinuxEvent::MANUAL_RESET, false);
  433. if (!m_PauseEvt || !m_SleepingEvt || !m_ResumeEvt || !m_WorkingEvt) {
  434. throw std::runtime_error("Work_Thread event creation failed");
  435. }
  436. MsgQueue<ResDataObject>* p;
  437. p = new MsgQueue<ResDataObject>;
  438. m_pWorkQueReq = (void *)p;
  439. p = new MsgQueue<ResDataObject>;
  440. m_pWorkQueRes = (void*)p;
  441. }
  442. Work_Thread::~Work_Thread() {
  443. MsgQueue<ResDataObject>* p;
  444. p = (MsgQueue<ResDataObject> *)m_pWorkQueReq;
  445. delete p;
  446. m_pWorkQueReq = NULL;
  447. p = (MsgQueue<ResDataObject> *)m_pWorkQueRes;
  448. delete p;
  449. m_pWorkQueRes = NULL;
  450. }
  451. bool Work_Thread::PopReqDataObject(ResDataObject& obj) {
  452. return (bool)((MsgQueue<ResDataObject> *)m_pWorkQueReq)->DeQueue(obj);
  453. }
  454. bool Work_Thread::PushReqDataObject(ResDataObject& obj) {
  455. if (WaitTheThreadEndSign(0))
  456. {
  457. return false;//not possible to send it
  458. }
  459. //printf("Thread:%d,push REQ one\n", GetCurrentThreadId());
  460. ((MsgQueue<ResDataObject> *)m_pWorkQueReq)->InQueue(obj);
  461. NotifyThreadWork();
  462. return true;
  463. }
  464. bool Work_Thread::PopResDataObject(ResDataObject& obj) {
  465. //printf("Thread:%d,Pop Res one\n", GetCurrentThreadId());
  466. return (bool)((MsgQueue<ResDataObject> *)m_pWorkQueRes)->DeQueue(obj);
  467. }
  468. bool Work_Thread::PushResDataObject(ResDataObject& obj) {
  469. if (WaitTheThreadEndSign(0))
  470. {
  471. return false;//not possible to send it
  472. }
  473. //printf("Thread:%d,push Res one\n",GetCurrentThreadId());
  474. ((MsgQueue<ResDataObject> *)m_pWorkQueRes)->InQueue(obj);
  475. NotifyThreadWork();
  476. return true;
  477. }
  478. bool Work_Thread::WaitForInQue(unsigned long m_Timeout) {
  479. std::vector<std::shared_ptr<LinuxEvent>> waitEvents = {
  480. m_ExitFlag,
  481. m_PauseEvt,
  482. m_WorkFlag ,
  483. 0,
  484. 0
  485. };
  486. waitEvents[3] = ((MsgQueue<ResDataObject> *)m_pWorkQueReq)->GetNotifyHandle();
  487. waitEvents[4] = ((MsgQueue<ResDataObject> *)m_pWorkQueRes)->GetNotifyHandle();
  488. int ret = LinuxEvent::WaitForMultipleEvents(waitEvents,0);
  489. if ((ret >= WAIT_OBJECT_0) && (ret <= WAIT_OBJECT_0 + 1))
  490. {
  491. return false;
  492. }
  493. if (((MsgQueue<ResDataObject> *)m_pWorkQueReq)->WaitForInQue(0) == WAIT_OBJECT_0)
  494. {
  495. return true;
  496. }
  497. if (((MsgQueue<ResDataObject> *)m_pWorkQueRes)->WaitForInQue(0) == WAIT_OBJECT_0)
  498. {
  499. return true;
  500. }
  501. ret = LinuxEvent::WaitForMultipleEvents(waitEvents,m_Timeout);
  502. if ((ret >= WAIT_OBJECT_0 + 2) && (ret < (WAIT_OBJECT_0 + 5)))
  503. {
  504. return true;
  505. }
  506. return false;
  507. }
  508. bool Work_Thread::CheckForPause() {
  509. DWORD retEvt = 0;
  510. bool ret = false;
  511. std::vector<std::shared_ptr<LinuxEvent>> waitExp = {
  512. m_ExitFlag,
  513. m_PauseEvt
  514. };
  515. retEvt = LinuxEvent::WaitForMultipleEvents(waitExp, 0);
  516. if (retEvt == WAIT_OBJECT_0 + 1)
  517. {
  518. m_WorkingEvt->ResetEvent();
  519. m_SleepingEvt->SetEvent();
  520. ret = true;
  521. }
  522. return ret;
  523. }
  524. bool Work_Thread::WaitforResume() {
  525. bool Exret = false;
  526. std::vector<std::shared_ptr<LinuxEvent>> wait = {
  527. m_ExitFlag,
  528. m_ResumeEvt
  529. };
  530. DWORD ret = 0;
  531. ret = LinuxEvent::WaitForMultipleEvents(wait, -1);
  532. if (ret == WAIT_OBJECT_0 + 1)
  533. {
  534. //拿到Resume
  535. m_SleepingEvt->ResetEvent();
  536. m_WorkingEvt->SetEvent();
  537. Exret = true;
  538. }
  539. return Exret;
  540. }
  541. bool Work_Thread::PauseThread(unsigned long timeout) {
  542. bool ret = false;
  543. DWORD retEvt = 0;
  544. std::vector<std::shared_ptr<LinuxEvent>> wait = {
  545. m_ExitFlag,
  546. m_SleepingEvt
  547. };
  548. if (pthread_self() == GetTID()) {
  549. return false;
  550. }
  551. if (!WaitTheThreadOnTheRun(0)) {
  552. return false;
  553. }
  554. m_PauseEvt->SetEvent();
  555. //get response
  556. retEvt = LinuxEvent::WaitForMultipleEvents(wait, timeout);
  557. if (retEvt == WAIT_OBJECT_0 + 1)
  558. {
  559. ret = true;
  560. }
  561. m_PauseEvt->ResetEvent();
  562. return ret;
  563. }
  564. bool Work_Thread::ResumeThread(unsigned long timeout) {
  565. bool ret = false;
  566. DWORD retEvt = 0;
  567. std::vector<std::shared_ptr<LinuxEvent>> wait = {
  568. m_ExitFlag,
  569. m_WorkingEvt
  570. };
  571. if (pthread_self() == GetTID()) {
  572. return false;
  573. }
  574. if (!WaitTheThreadOnTheRun(0)) {
  575. return false;
  576. }
  577. m_ResumeEvt->SetEvent();
  578. //get response
  579. retEvt = LinuxEvent::WaitForMultipleEvents(wait, timeout);
  580. if (retEvt == WAIT_OBJECT_0 + 1)
  581. {
  582. ret = true;
  583. }
  584. m_ResumeEvt->ResetEvent();
  585. return ret;
  586. }
  587. // Ccos_Thread 实现
  588. Ccos_Thread::Ccos_Thread() {}
  589. Ccos_Thread::~Ccos_Thread() {}