我如何等待所有线程完成 [英] How do I wait until all threads have completed

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

问题描述

我想创建一个测试工具,模拟执行单个线程的多个并发

用户。当用户指定所需线程的数量时,我希望在

运行时确定。当这是
开始时,我想在主线程中等待,直到所有工作人员完成并计算结果......一个问题......我无法想象

如何在更新我的时间之前等待所有线程完成。


有谁知道如何做到这一点?

I would like to create a test harness that simulates multiple concurrent
users executing an individual thread. I would like this to be determined at
runtime when the user specifies the number of desired threads. When this is
kicked off, I would like to wait in the primary thread until all worker
threads have completed and time the result... one problem... I can''t figure
out how to wait for all threads to complete prior to updating my timing.

Does anyone know how this could be done?

推荐答案

Thirsty Traveler写道:
Thirsty Traveler wrote:
我想创建一个模拟多个并行用户执行的测试工具个人主题。我希望在用户指定所需线程的数量时在运行时确定。当这个开始时,我想在
主线程中等待,直到所有工作线程都已完成并计时结果......一个问题......我无法弄清楚如何等待所有
线程在更新我的时间之前完成。
有谁知道如何做到这一点?
I would like to create a test harness that simulates multiple
concurrent users executing an individual thread. I would like this to
be determined at runtime when the user specifies the number of
desired threads. When this is kicked off, I would like to wait in the
primary thread until all worker threads have completed and time the
result... one problem... I can''t figure out how to wait for all
threads to complete prior to updating my timing.
Does anyone know how this could be done?




1.累积线程句柄对于您创建的所有线程。

2.在您创建了所有线程之后,等待每个线程

逐个处理任何线程订单。


当最后一次等待完成后,你的所有线程都已终止。


-cd



1. Accumulate thread handles for all of the threads that you create.
2. After you''ve created all of the threads, wait on each of the thread
handles one by one in any order.

When the last wait is completed, all of your threads have terminated.

-cd


我该如何等待?你有代码片段吗?


" Carl Daniel [VC ++ MVP]" < cp ***************************** @ mvps.org.nospam>

写道消息新闻:uV ************** @ TK2MSFTNGP02.phx.gbl ...
How do I wait? Do you have a code snippet?

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uV**************@TK2MSFTNGP02.phx.gbl...
Thirsty Traveler写道:
Thirsty Traveler wrote:
我想要创建一个测试工具,模拟执行单个线程的多个并发用户。我希望在用户指定所需线程的数量时在运行时确定。当这个开始时,我想在
主线程中等待,直到所有工作线程都已完成并计时结果......一个问题......我无法弄清楚如何在更新我的时间之前等待所有
线程完成。
有谁知道如何做到这一点?
I would like to create a test harness that simulates multiple
concurrent users executing an individual thread. I would like this to
be determined at runtime when the user specifies the number of
desired threads. When this is kicked off, I would like to wait in the
primary thread until all worker threads have completed and time the
result... one problem... I can''t figure out how to wait for all
threads to complete prior to updating my timing.
Does anyone know how this could be done?



1.累积所有的线程句柄你创建的线程。
2.创建完所有线程后,按任意顺序逐个等待每个线程
句柄。

最后等待完成,所有线程都已终止。

-cd



1. Accumulate thread handles for all of the threads that you create.
2. After you''ve created all of the threads, wait on each of the thread
handles one by one in any order.

When the last wait is completed, all of your threads have terminated.

-cd



Thirsty Traveler写道:
Thirsty Traveler wrote:
我该如何等待?你有代码片段吗?

Carl Daniel [VC ++ MVP]" < cp ***************************** @ mvps.org.nospam>
写在留言新闻:uV ************** @ TK2MSFTNGP02.phx.gbl ...
How do I wait? Do you have a code snippet?

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uV**************@TK2MSFTNGP02.phx.gbl...
Thirsty Traveler写道:
Thirsty Traveler wrote:
我想创建一个测试线束模拟执行单个线程的多个并发用户。我希望在用户指定所需线程的数量时在运行时确定。当这个开始时,我想在
主线程中等待,直到所有工作线程都已完成并计时结果......一个问题......我无法弄清楚如何在更新我的时间之前等待所有
线程完成。
有谁知道如何做到这一点?
I would like to create a test harness that simulates multiple
concurrent users executing an individual thread. I would like this to
be determined at runtime when the user specifies the number of
desired threads. When this is kicked off, I would like to wait in the
primary thread until all worker threads have completed and time the
result... one problem... I can''t figure out how to wait for all
threads to complete prior to updating my timing.
Does anyone know how this could be done?



1.累积所有的线程句柄你创建的线程。
2.创建完所有线程后,按任意顺序逐个等待每个线程
句柄。

最后等待完成,所有线程都已终止。

-cd



1. Accumulate thread handles for all of the threads that you create.
2. After you''ve created all of the threads, wait on each of the thread
handles one by one in any order.

When the last wait is completed, all of your threads have terminated.

-cd







在System.Threading中查看WaitHandle类。它包含一个名为WaitAll的静态方法,这将阻止调用线程,直到

所有WaitHandle传入时都会发出参数信号。


你需要做的是为每个线程创建一个AutoResetEvent,当

a线程完成时,调用AutoResetEvent上的''Set''方法。 br />
AutoResetEvent继承自WaitHandle,所以你要做的就是将一个数组

的AutoResetEvent传递给WaitAll方法,当每一个
$ b时$ b AutoResetEvent发出信号(即Set已被调用)线程调用

WaitAll将恢复。


希望这有帮助,

- Tom Spink



Hi,

Take a look at the WaitHandle class, in System.Threading. It contains a
static method called WaitAll, and this will block the calling thread until
all the WaitHandle''s passed in as the parameter are signalled.

What you''ll need to do is create an AutoResetEvent for each thread, and when
a thread completes, call the ''Set'' method on the AutoResetEvent.
AutoResetEvent inherits from WaitHandle, so all you do is pass an array of
the AutoResetEvent''s into the WaitAll method, and when each and every
AutoResetEvent is signalled (i.e. Set has been called) the thread calling
WaitAll will resume.

Hope this helps,
-- Tom Spink


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

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