VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 979|回复: 2

多线程编程线程同步,Events同步

[复制链接]
53_avatar_middle
最佳答案
0 
donate_vip 发表于 2021-7-28 14:21:11 | 显示全部楼层 |阅读模式
Events 线程同步不太会用,能不能给讲个示例?




上一篇:编写可变参数函数模板失败
下一篇:VC/MFC
62_avatar_middle
最佳答案
0 
在线会员 发表于 2021-7-28 21:58:51 | 显示全部楼层
官网有更加详细的解释和用法,这个是官网例子
  1. // The following demonstrates trivial usage of the CEvent class.
  2. // A CEvent object is created and passed as a parameter to another
  3. // thread.  The other thread will wait for the event to be signaled
  4. // and then exit

  5. UINT __cdecl MyThreadProc(LPVOID lpParameter)
  6. {
  7.    CEvent *pEvent = (CEvent *)(lpParameter);
  8.    VERIFY(pEvent != NULL);

  9.    // Wait for the event to be signaled
  10.    ::WaitForSingleObject(pEvent->m_hObject, INFINITE);

  11.    // Terminate the thread
  12.    ::AfxEndThread(0, FALSE);
  13.    return 0L;
  14. }

  15. void CEvent_Test()
  16. {
  17.    // Create the CEvent object that will be passed to the thread routine
  18.    CEvent *pEvent = new CEvent(FALSE, FALSE);

  19.    // Create a thread that will wait on the event
  20.    CWinThread *pThread;
  21.    pThread = ::AfxBeginThread(&MyThreadProc, pEvent, 0, 0, CREATE_SUSPENDED, NULL);
  22.    pThread->m_bAutoDelete = FALSE;
  23.    pThread->ResumeThread();

  24.    // Signal the thread to do the next work item
  25.    pEvent->SetEvent();

  26.    // Wait for the thread to consume the event and return
  27.    ::WaitForSingleObject(pThread->m_hThread, INFINITE);
  28.    delete pThread;
  29.    delete pEvent;
  30. }
复制代码

53_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-7-29 14:41:17 | 显示全部楼层
谢谢,现在明白了
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-10 09:13

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表