473,473 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A question about WaitForMultipleObjects

Hi all,
I'm using WaitForMultipleObjects, I give an array of handles and give it
FALSE in bWaitAll param. What I want to do is, I spawn few threads upto N,
than I wait for any of them to end, when any one or more of them ends, I
want to spawn as many threads as were ended. So Its going to be a while
loop, inside that is going to be a WaitForMultipleObjects with array of
thread handles. So now my question is that when WaitForMultipleObjects
returns, how do I know which handle was the one that got signaled and how do
I fit in the handle(s) so that another WaitForMultipleObjects can be
performed on the array.

Using visual c++ 2k5 on windows xp sp2 (all native c++ code);

Regards,

...ab
Aug 5 '08 #1
3 2023
Hi Abubakar,
Hi all,
I'm using WaitForMultipleObjects, I give an array of handles and give
it FALSE in bWaitAll param. What I want to do is, I spawn few threads
upto N, than I wait for any of them to end, when any one or more of
them ends, I want to spawn as many threads as were ended.
Why do you want to start/end/restart threads?
This sounds like a bad design.
Start threads to act as a thread pool that lives as long as your process
needs the threads to do their work.
A typical design pattern is that the thread function is waiting in a loop
on some object to be signalled. When signalled it might read some
arguments from a task queue to process that task. One task or one
signal would be an indicator that the process need to shut down and
the thread should leave its loop.
An efficient signal/queue technique are windows IO completion ports.
Use google or msdn to get the details.

--
SvenC
Aug 5 '08 #2
On Tue, 5 Aug 2008 13:11:06 +0500, "Abubakar" <q@y.comwrote:
>Hi all,
I'm using WaitForMultipleObjects, I give an array of handles and give it
FALSE in bWaitAll param. What I want to do is, I spawn few threads upto N,
than I wait for any of them to end, when any one or more of them ends, I
want to spawn as many threads as were ended. So Its going to be a while
loop, inside that is going to be a WaitForMultipleObjects with array of
thread handles. So now my question is that when WaitForMultipleObjects
returns, how do I know which handle was the one that got signaled and how do
I fit in the handle(s) so that another WaitForMultipleObjects can be
performed on the array.
The thread pool suggestion may be the right approach, but to answer your
questions, a successful WFMO returns WAIT_OBJECT_0+N, where N indexes the
handle array. More handles may be signaled than just that one, but I
wouldn't try to detect this, as your WFMO loop will handle it fine, and you
can't beat the race condition inherent in trying to detect this with (say)
WFSO and zero timeout. If you have a new handle to wait on, you can simply
replace the old one in your handle array.

--
Doug Harrison
Visual C++ MVP
Aug 5 '08 #3
Hmm, just made a thread pool thing. Noticed it was easier than the way I
wanted to go. Thanks.

"SvenC" <Sv***@nospam.nospamwrote in message
news:92**********************************@microsof t.com...
Hi Abubakar,
>Hi all,
I'm using WaitForMultipleObjects, I give an array of handles and give
it FALSE in bWaitAll param. What I want to do is, I spawn few threads
upto N, than I wait for any of them to end, when any one or more of
them ends, I want to spawn as many threads as were ended.

Why do you want to start/end/restart threads?
This sounds like a bad design.
Start threads to act as a thread pool that lives as long as your process
needs the threads to do their work.
A typical design pattern is that the thread function is waiting in a loop
on some object to be signalled. When signalled it might read some
arguments from a task queue to process that task. One task or one
signal would be an indicator that the process need to shut down and
the thread should leave its loop.
An efficient signal/queue technique are windows IO completion ports.
Use google or msdn to get the details.

--
SvenC

Aug 7 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
8
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
5
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is...
0
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew...
1
by: Madhu Gopinathan | last post by:
Hi, I need to implement my own ThreadPool class because I want to have control over the threads and their lifetimes. The ThreadPool class will take care of thread recovery in case threads lock up...
3
by: Pinux | last post by:
Hi, I am writing a multi-threads encryption application. The idea of the code is to create a number of threads to encrypt files. I have a thread pool say the maximum threads is 10. If the number...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.