Hi all
this question may be obvious to many but still i wanted to ask u because i
am not very clear about it.
Once i have created a pool of worker threads (not the kernel mode worker
threads , its just any pool of threads) for doing some work which will keep
on coming in a dynamic
basis. How should i tell each worker thread what to do. I mean i want to
allocate threads to work in a dynamic way and still wnat the pool of threads
to be created before hand.
thanks
Mayank
Use a model similar to the kernel worker threads. You create a ‘workitem’
context record that you put on a driver global queue and then signal your
thread pool to go process the queue. Generally you only need to create
private worker thread pools if your processing interferes with the paging
system/storage stack, or your tasks run for a very long time. Otherwise the
kernel worker thread pool is just dandy.
-----Original Message-----
From: Mayank Kumar [mailto:xxxxx@intersolutions.stpn.soft.net]
Sent: Friday, May 23, 2003 12:59 PM
To: NT Developers Interest List
Subject: [ntdev] THREAD POOL HOWTO
Hi all
this question may be obvious to many but still i wanted to ask u because i
am not very clear about it. Once i have created a pool of worker threads
(not the kernel mode worker threads , its just any pool of threads) for
doing some work which will keep on coming in a dynamic basis. How should i
tell each worker thread what to do. I mean i want to allocate threads to
work in a dynamic way and still wnat the pool of threads to be created
before hand.
thanks
Mayank
You are currently subscribed to ntdev as: xxxxx@stratus.com To
unsubscribe send a blank email to xxxxx@lists.osr.com
Use a signaling mechanism (like an EVENT) and have them block on the
signaling mechanism. When you insert something into the queue, signal there
is work to do. When the thread is scheduled (because it was signaled) have
it remove an entry from the queue.
If the queue is empty, have the thread clear the signal (like resetting the
EVENT) and then check the queue one last time (to eliminate the race
condition between checking and clearing the signal.)
Regards,
Tony
Tony Mason
Consulting Partner
OSR Open Systems Resources, Inc.
http://www.osr.com
-----Original Message-----
From: Mayank Kumar [mailto:xxxxx@intersolutions.stpn.soft.net]
Sent: Friday, May 23, 2003 12:59 PM
To: NT Developers Interest List
Subject: [ntdev] THREAD POOL HOWTO
Hi all
this question may be obvious to many but still i wanted to ask u because i
am not very clear about it.
Once i have created a pool of worker threads (not the kernel mode worker
threads , its just any pool of threads) for doing some work which will keep
on coming in a dynamic
basis. How should i tell each worker thread what to do. I mean i want to
allocate threads to work in a dynamic way and still wnat the pool of threads
to be created before hand.
thanks
Mayank
You are currently subscribed to ntdev as: xxxxx@osr.com
To unsubscribe send a blank email to xxxxx@lists.osr.com
Let me add something to what’s been said so far. Inasmuch as you’ve
asked about a pool of worker threads, you have the problem of deciding
which thread to do the work of a specific work item. I suggest there be
a master thread, which is always signalled by someone who has queued up
a work item. The master thread wakes up, dequeues the item, selects a
worker thread and signals it (that is, all the worker threads are
waiting). The worker thread does the work.
When it comes to selecting a worker thread, there are various possible
strategies. If it’s important that the work be effected as soon as
possible, it may be best to signal a waiting thread (assuming there are
any; if not, that’s a problem that has to be taken care of). If it’s not
so important, and if one wants a little optimization, the master can
look for an active thread and queue up the work item to the worker’s own
per-thread queue; of course, in this approach there is a race condition,
in that the master may queue up the work after the point when the worker
last checks the queue, but a little interlocked-exchange logic will
address the race condition.
–
If replying by e-mail, please remove “nospam.” from the address.
James Antognini
> which thread to do the work of a specific work item. I suggest there
be
a master thread, which is always signalled by someone who has queued
up
a work item. The master thread wakes up, dequeues the item, selects
a
worker thread and signals it (that is, all the worker threads are
waiting). The worker thread does the work.
Master thread is extra. A pool of peer threads are waiting on
synchronization event, and works like the following:
- wait
- return from wait resets the event
- take the first item from the queue
- if there are more items on the queue - signal the event once more
for the next thread.
Surely, your ExQueueWorkItem version will add the item to the list and
set the event.
so important, and if one wants a little optimization, the master can
look for an active thread and queue up the work item to the worker’s
own
per-thread queue;
For what? Why queue the item to already busy thread if there are idle
ones?
Max
Handing off a work item to an active thread might be good if the cost of
waking up another thread is high and the likely wait time – before the
work item is actually handled – is tolerably low. In a case where the
number of work items per second is sufficiently high, this could be a
valuable optimization. In my book, it’s sloppy design to have one thread
active for each work item, all other things being equal. I’ve seen that,
and I’ve done that, but only because there wasn’t the luxury of creating
and implementing a tighter design.
–
If replying by e-mail, please remove “nospam.” from the address.
James Antognini
There is a worker thread pool library in user mode. You should use that. A
very optimal way to convey workitems to a pool of worker threads is to use
completion ports. The underlying queue for worker threads in the kernel is
the same as the completion port queue.
–
Nar Ganapathy
Windows Core OS group
This posting is provided “AS IS” with no warranties, and confers no rights.
“Mayank Kumar” wrote in message
news:xxxxx@ntdev…
>
> Hi all
> this question may be obvious to many but still i wanted to ask u because i
> am not very clear about it.
> Once i have created a pool of worker threads (not the kernel mode worker
> threads , its just any pool of threads) for doing some work which will
keep
> on coming in a dynamic
> basis. How should i tell each worker thread what to do. I mean i want to
> allocate threads to work in a dynamic way and still wnat the pool of
threads
> to be created before hand.
>
>
> thanks
> Mayank
>
>
>
>
>
>