Windows线程等待方法 [英] Windows Threading Wait Method

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

问题描述

我正在创建一个线程类来封装窗口线程方法。我试图创建一个方法,使应用程序等待线程完成,然后退出应用程序。如果我使用一个while循环和布尔标志,它的工作,但显然它尖峰我的CPU使用,它只是不理想。

I'm creating a thread class to encapsulat the windows thread methods. I'm trying to create a method that makes the application wait for the thread to complete before it exits the application. If I use a while loop and boolean flag, it works but obviously it spikes my CPU use and its just not ideal.

您将使用什么方式来等待完成的线程? (我不是真的在这里寻找代码,只是区域看)

What ways would you use to wait for the completion of a thread? (i'm not really looking for code here, just areas to look into)

推荐答案

使用CreateThread后得到一个线程句柄,将其传递到Win32 API WaitForSingleObject

After you use CreateThread to get a thread handle, pass it into the Win32 API WaitForSingleObject:

WaitForSingleObject(threadhandle, INFINITE);

如果你不使用CreateThread(因为你使用另一个线程包),或者你的线程总是alive ...

If you do not use CreateThread (because you use another threading package), or perhaps your thread is always alive...

然后你仍然可以使用WaitForSingleObject。只需使用Win32 API CreateEvent 首先创建一个事件,然后等待该事件要使用WaitForSingleObject设置。在您的主题结束时,使用 SetEvent 设置活动,您可以请使用重置事件重置事件。

Then you can still use WaitForSingleObject. Just create an event first with the Win32 API CreateEvent, and wait for the event to be set with WaitForSingleObject. At the end of your thread set the event with SetEvent and you can reset the event with ResetEvent.

大多数线程包都有自己的方式来等待线程。像 boost :: thread 一样,您可以使用.join( )或 boost ::条件

Most threading packages though will have their own way to wait for a thread. Like in boost::thread you can use .join() or a boost::condition.

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

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