等待多个线程 [英] Waiting on multiple Threads

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

问题描述

大家好,


排队等待完成线程数的最佳方法是什么。

这个问题很简单VC ++ 6但我发现在VB.NET中很难解决

。我的计算大约每个2分钟(2.8 Ghz Xeon和

服务器范围从2到8个处理器)(给出或大约需要15秒)。我有8个

计算要做(在另一个应用程序中我有121个)所有相同的计算

只是不同的数据。但其余的处理都无法继续,直到

所有的计算完成。


所以这里是我想要使用的算法(写自C ++实现

立场){为了便于阅读我遗漏了任何/所有关于

的讨论,队列操作用于安排可以改变为什么的运行

合适}

-------------------------------- ----------------------------------

获取用户pref并发线程数...

for循环(到并发线程数)

启动一个线程(带数据)

添加到等待列表

while true

WaitForMultipleObjects(... threads ...)

如果要处理更多线程

启动数据线程

加入等待名单

否则如果所有线程都已完成

break

-------- -------------------------------------------------- --------


问题我有我找到.NET相当于

WaitForMultipleObjects。线程可能无法按顺序完成,或者可能同时完成
。我正在玩一个代表的想法,

基本上会有while循环的功能,但看起来好像是b $ b意大利面条给我编码。


我们将非常感谢您能想到的任何想法或例子。


提前致谢,

Mike




------------------------------------ ----------------------

Mike Elledge

解决方案

<" MikeE" < mikeDOTelledgeATattbiDOTcom>>写道:

排队等待完成线程数的最佳方法是什么。
这个问题在VC ++ 6中是微不足道的,但我发现很难在VB.NET中解决。我的计算大约每个2分钟(2.8 Ghz Xeon和
服务器范围从2到8个处理器)(给出或大约需要15秒)。我有8组计算要做(在另一个应用程序中我有121个)所有相同的计算
只是不同的数据。但其余的处理都无法继续,直到所有的计算完成。




这很简单 - 只需调用Join()线程。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


Jon,


没有加入()阻止,直到该特定线程为止完成(和

WaitForSingleObject一样)?如果是这样,那就不会给我正确的安排

吧?当任何当前线程结束时,我需要在队列中安排下一个

,并且我不能安排(即,start())下一个UNTIL之一

MAX#完成。


如果我错误加入(),请纠正我。


谢谢,< br $>
ME


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

<" MikeE" < mikeDOTelledgeATattbiDOTcom>>写道:

什么是排队等待线程数完成的最佳方式。
这个问题在VC ++ 6中是微不足道的但是我找到了在VB.NET中很难解决问题。我的计算大约每个2分钟(2.8 Ghz Xeon和
服务器范围从2到8个处理器)(给出或大约需要15秒)。我有8套
要做的计算(在另一个应用程序中我有121个)所有相同的
只是不同的数据。但其余的处理无法继续
直到
所有的计算完成。



这很简单 - 只需在所有计算上调用Join()线程。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复该群组,请不要给我发邮件



<" MikeE" < mikeDOTelledgeATattbiDOTcom>>写道:

在特定线程完成之前,是否加入()阻塞(与
WaitForSingleObject相同)?


是的。

如果是这样,那么那不能给我正确的时间安排吗?


Nope。

当任何当前线程结束时,我需要在队列中安排下一个,我不能时间表(即,开始())下一个直到MAX#完成其中一个。

如果我对Join()的错误,请纠正我。




也许我误解了你。我以为你已经创建了一堆

个线程,开始运行它们,然后想知道他们什么时候全部完成了b
。在这种情况下,依次调用每个上面的Join()确实就是你想要的。


如果这不是你的''在做,你需要多给一些关于你在做什么的信息。


-

Jon Skeet - < sk *** @ pobox.com>
http ://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


Hi all,

What''s the best way to queue up and wait for number of threads to complete.
This problem was trivial in VC++ 6 but I''m finding it rather hard to solve
in VB.NET. My calculations run about 2 mins each (on a 2.8 Ghz Xeon and the
server range from 2 to 8 processors) (give or take about 15 secs). I have 8
sets of calculations to do (in another app I have 121) all the same calc
just different data. But the rest of the processing cannot continue until
ALL of the calculations are done.

so here''s the algorithm I want to use (written from the C++ implentation
standpoint) {for ease of reading i have left out any/all discussion about
the queue ops used to schedule the runs which can change to what ever is
appropriate}
------------------------------------------------------------------
get user pref on number of concurrent threads...
for loop (to number of concurrent threads)
start a thread (with data)
add to wait list
while true
WaitForMultipleObjects(...threads...)
if more threads to process
launch thread with data
add to wait list
else if all threads finished
break
------------------------------------------------------------------

The problem I''m having is finding the .NET equivalent to
WaitForMultipleObjects. The threads might not complete in order or might
complete at the same time. I was toying with the idea of a delegate that
would basically have the functionality of the while loop, but seemed like
spaghetti coding to me.

Any ideas or examples you can think of would be greatly appreciated.

Thanks in advance,
Mike



----------------------------------------------------------
Mike Elledge

解决方案

<"MikeE" <mikeDOTelledgeATattbiDOTcom>> wrote:

What''s the best way to queue up and wait for number of threads to complete.
This problem was trivial in VC++ 6 but I''m finding it rather hard to solve
in VB.NET. My calculations run about 2 mins each (on a 2.8 Ghz Xeon and the
server range from 2 to 8 processors) (give or take about 15 secs). I have 8
sets of calculations to do (in another app I have 121) all the same calc
just different data. But the rest of the processing cannot continue until
ALL of the calculations are done.



That''s easy then - just call Join() on all the threads.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Jon,

Doesn''t Join() block until that particular thread is done (the same as
WaitForSingleObject)? If so, then that won''t give me the proper scheduling
right? When any of the current threads end, I need to schedule the next in
the queue, and I can''t schedule (i.e., start()) the next one UNTIL one of
the MAX # finishes.

Please correct me if i''m wrong about Join().

Thanks,
ME

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

<"MikeE" <mikeDOTelledgeATattbiDOTcom>> wrote:

What''s the best way to queue up and wait for number of threads to
complete.
This problem was trivial in VC++ 6 but I''m finding it rather hard to
solve
in VB.NET. My calculations run about 2 mins each (on a 2.8 Ghz Xeon and
the
server range from 2 to 8 processors) (give or take about 15 secs). I have
8
sets of calculations to do (in another app I have 121) all the same
calc
just different data. But the rest of the processing cannot continue
until
ALL of the calculations are done.



That''s easy then - just call Join() on all the threads.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



<"MikeE" <mikeDOTelledgeATattbiDOTcom>> wrote:

Doesn''t Join() block until that particular thread is done (the same as
WaitForSingleObject)?
Yes.
If so, then that won''t give me the proper scheduling right?
Nope.
When any of the current threads end, I need to schedule the next in
the queue, and I can''t schedule (i.e., start()) the next one UNTIL one of
the MAX # finishes.

Please correct me if i''m wrong about Join().



Maybe I''ve misunderstood you. I thought you had created a bunch of
threads, started them running, and then wanted to know when they''d all
finished. In that case, calling Join() on each of them in turn does
exactly what you want.

If that''s not what you''re doing, you''ll need to give a bit more
information about what you''re doing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


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

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