MFC中的多线程 [英] Multithreading in MFC

查看:92
本文介绍了MFC中的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是应用程序,则使用AfxBeginthread创建线程,并使用CSemephore进行同步,如果否,则使用.工作线程的数量是不固定的(即在运行时依赖).

If the application, threads are created using AfxBeginthread and for synchronization CSemephore is used then if the no. of worker threads is not fixed (i.e dependends in runtime).
How the main thread waits for all the worker thread to finish there execution first before terminate itself?

推荐答案

您可以执行以下操作:

You could do the operations below:


  1. CREATE_SUSPENDED标志添加到对AfxBeginThread
  2. 调用DuplicateHandle的任何调用中,以获取先前对CWinThread对象的m_hThread的副本. >
  3. 对先前调用AfxBeginThread

  1. add the CREATE_SUSPENDED flag to any call to AfxBeginThread
  2. call DuplicateHandle to get a copy of the m_hThread of each CWinThread object returned by the previous call to AfxBeginThread
  3. call ResumeThread on each CWinThread object returned by the previous call to AfxBeginThread


返回的每个CWinThread对象调用ResumeThread
这将确保您获得创建的每个线程的句柄(您应该将每个线程都挂起,因为CWinThread将在线程终止时自动删除,并且您需要在完成初始化之前线程不终止)步骤).

当您要终止主线程时,请执行以下操作:



This will ensure that you get an handle to each thread you create (you should create each thread suspended because CWinThread will auto-delete on thread termination, and you need that the thread doesn''t terminate before that you have completed your initialization steps).

When you are about to terminate your main thread, do the following:


  1. WaitForMultipleObject进行调用,将包含已创建线程的所有句柄的数组传递给它
  2. 对先前复制的每个句柄进行调用CloseHandle

  1. make a call to WaitForMultipleObject passing it an array with all the handles of the threads you have created
  2. call CloseHandle fore each handle that you have previously duplicated


这篇关于MFC中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆