I would like to have a limited pool of objects (the objects are expensive to
create) that can be enlisted to process items in a queue. Moreover, I
would like to be able to have the processing (time-consuming) occur on a
background thread.
I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
* is my approach a reasonable one?
Thanks,
Craig Buchanan
Here's my psedo-code:
Sub ProcessQueue
Do While Queue.Count 0
'wait for next available worker
Worker = ObjectPool.getF reeWorker()
'assign next item in the queue to the worker
Worker.Item = Queue.Dequeue
'run time-consuming process in background
ThreadPool.Queu eUserWorkItem(A ddressOf ProcessItem, Worker)
Loop
End Sub
Sub ProcessItem(Sta te As Object)
Dim WorkerAs Worker= CType(State,Wor ker)
Try
'process item
Worker.Process( )
Catch ex As Exception
'return item to queue
Queue.Enqueue(W orker.Item)
Finally
'return worker to pool
ObjectPool.retu rnWorker(Worker )
End Try
End Sub 10 1407
I've written this code more times than I can count, so I hope it's a decent
pattern to follow.
One of these days soon, I'll post my ObjectPool<Tcla sses to my blog, along
with some instructions on how to use them. This problem seems to come up
very frequently when writing server applications - we've got dozens of
"expensive" object pools that we maintain. Everything from AD & LDAP
connections, to pinned byte[] buffers for use in reading & writing to
Sockets (to avoid heap fragmentation).
It's got a nice robust mechanism for checking objects in & out of the pool
(using IDisposable so that 'using' blocks can be employed, and also using
Object Resurrection so that the Finalizer is run, the expensive object
doesn't get lost) and its very easy to use.
You wait problem, as you've described it, I usually see it solved with the
Monitor.Pulse / PulseAll pattern. Worker threads are kept blocked in a
Monitor, and when data is added to the queue, the Monitor is Pulsed
realeasing one of the waiting threads. You've got to be carefull of race
conditions, but it's a very well worn pattern.
--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP http://www.coversant.com/blogs/cmullins
"Craig Buchanan" <so*****@somewh ere.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>I would like to have a limited pool of objects (the objects are expensive to create) that can be enlisted to process items in a queue. Moreover, I would like to be able to have the processing (time-consuming) occur on a background thread.
I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
* is my approach a reasonable one?
Thanks,
Craig Buchanan
Here's my psedo-code:
Sub ProcessQueue
Do While Queue.Count 0
'wait for next available worker
Worker = ObjectPool.getF reeWorker()
'assign next item in the queue to the worker
Worker.Item = Queue.Dequeue
'run time-consuming process in background
ThreadPool.Queu eUserWorkItem(A ddressOf ProcessItem, Worker)
Loop
End Sub
Sub ProcessItem(Sta te As Object)
Dim WorkerAs Worker= CType(State,Wor ker)
Try
'process item
Worker.Process( )
Catch ex As Exception
'return item to queue
Queue.Enqueue(W orker.Item)
Finally
'return worker to pool
ObjectPool.retu rnWorker(Worker )
End Try
End Sub
Chris-
Thanks for the reply. Does your approach also use the ThreadPool to process
the long-running job?
Any chance I could see the code? craig dot buchanan at cogniza dot com. is
this your blog: http://instructors.cwrl.utexas.edu/jesson/?q=blog/61 ?
Thanks,
Craig
"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:es******** ********@TK2MSF TNGP03.phx.gbl. ..
I've written this code more times than I can count, so I hope it's a
decent pattern to follow.
One of these days soon, I'll post my ObjectPool<Tcla sses to my blog,
along with some instructions on how to use them. This problem seems to
come up very frequently when writing server applications - we've got
dozens of "expensive" object pools that we maintain. Everything from AD &
LDAP connections, to pinned byte[] buffers for use in reading & writing to
Sockets (to avoid heap fragmentation).
It's got a nice robust mechanism for checking objects in & out of the pool
(using IDisposable so that 'using' blocks can be employed, and also using
Object Resurrection so that the Finalizer is run, the expensive object
doesn't get lost) and its very easy to use.
You wait problem, as you've described it, I usually see it solved with the
Monitor.Pulse / PulseAll pattern. Worker threads are kept blocked in a
Monitor, and when data is added to the queue, the Monitor is Pulsed
realeasing one of the waiting threads. You've got to be carefull of race
conditions, but it's a very well worn pattern.
--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP http://www.coversant.com/blogs/cmullins
"Craig Buchanan" <so*****@somewh ere.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>>I would like to have a limited pool of objects (the objects are expensive to create) that can be enlisted to process items in a queue. Moreover, I would like to be able to have the processing (time-consuming) occur on a background thread.
I have two questions: * what is a good way to block the queue loop while it waits for a free worker? * is my approach a reasonable one?
Thanks,
Craig Buchanan
Here's my psedo-code:
Sub ProcessQueue
Do While Queue.Count 0
'wait for next available worker Worker = ObjectPool.getF reeWorker()
'assign next item in the queue to the worker Worker.Item = Queue.Dequeue
'run time-consuming process in background ThreadPool.Queu eUserWorkItem(A ddressOf ProcessItem, Worker)
Loop
End Sub
Sub ProcessItem(Sta te As Object)
Dim WorkerAs Worker= CType(State,Wor ker)
Try
'process item Worker.Process( )
Catch ex As Exception
'return item to queue Queue.Enqueue(W orker.Item)
Finally
'return worker to pool ObjectPool.retu rnWorker(Worker )
End Try
End Sub
Chris-
I'm wondering if it would make sense for the ObjectPool to drive the process
of 'draining' the queue. Think of how bank tellers process a line of
customers.
Perhaps there would be an event (or delegate) that would fire when an item
in the pool becomes free. in the event's code, the worker would grab the
next time in the queue.
Seems like there would need to be a method on the ObjectPool to signal this
process to begin. Perhaps it could listen to an event raised by
queue.enqueue() .
Thoughts?
"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:es******** ********@TK2MSF TNGP03.phx.gbl. ..
I've written this code more times than I can count, so I hope it's a
decent pattern to follow.
One of these days soon, I'll post my ObjectPool<Tcla sses to my blog,
along with some instructions on how to use them. This problem seems to
come up very frequently when writing server applications - we've got
dozens of "expensive" object pools that we maintain. Everything from AD &
LDAP connections, to pinned byte[] buffers for use in reading & writing to
Sockets (to avoid heap fragmentation).
It's got a nice robust mechanism for checking objects in & out of the pool
(using IDisposable so that 'using' blocks can be employed, and also using
Object Resurrection so that the Finalizer is run, the expensive object
doesn't get lost) and its very easy to use.
You wait problem, as you've described it, I usually see it solved with the
Monitor.Pulse / PulseAll pattern. Worker threads are kept blocked in a
Monitor, and when data is added to the queue, the Monitor is Pulsed
realeasing one of the waiting threads. You've got to be carefull of race
conditions, but it's a very well worn pattern.
--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP http://www.coversant.com/blogs/cmullins
"Craig Buchanan" <so*****@somewh ere.comwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>>I would like to have a limited pool of objects (the objects are expensive to create) that can be enlisted to process items in a queue. Moreover, I would like to be able to have the processing (time-consuming) occur on a background thread.
I have two questions: * what is a good way to block the queue loop while it waits for a free worker? * is my approach a reasonable one?
Thanks,
Craig Buchanan
Here's my psedo-code:
Sub ProcessQueue
Do While Queue.Count 0
'wait for next available worker Worker = ObjectPool.getF reeWorker()
'assign next item in the queue to the worker Worker.Item = Queue.Dequeue
'run time-consuming process in background ThreadPool.Queu eUserWorkItem(A ddressOf ProcessItem, Worker)
Loop
End Sub
Sub ProcessItem(Sta te As Object)
Dim WorkerAs Worker= CType(State,Wor ker)
Try
'process item Worker.Process( )
Catch ex As Exception
'return item to queue Queue.Enqueue(W orker.Item)
Finally
'return worker to pool ObjectPool.retu rnWorker(Worker )
End Try
End Sub
On Mar 27, 3:37 pm, "Craig Buchanan" <some...@somewh ere.comwrote:
I would like to have a limited pool of objects (the objects are expensive to
create) that can be enlisted to process items in a queue. Moreover, I
would like to be able to have the processing (time-consuming) occur on a
background thread.
I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
The best way to block the loop is to block the getFreeWorker method
until a free worker is available. The pattern you're looking for is a
producer-consumer queue or blocking queue. Look for the
ProducerConsume r class in the following article. The Produce and
Consume methods in that class would map directly to your returnWorker
and getFreeWorker methods respectively. I know the examples in the
article are in C#, but you'll get the idea. http://www.yoda.arachsys.com/csharp/...eadlocks.shtml
* is my approach a reasonable one?
Yes, I think it is reasonable. There might be some minor changes I
would change regarding your general pattern, but nothing worth
mentioning.
Thanks,
Craig Buchanan
Here's my psedo-code:
Sub ProcessQueue
Do While Queue.Count 0
'wait for next available worker
Worker = ObjectPool.getF reeWorker()
'assign next item in the queue to the worker
Worker.Item = Queue.Dequeue
'run time-consuming process in background
ThreadPool.Queu eUserWorkItem(A ddressOf ProcessItem, Worker)
Loop
End Sub
Sub ProcessItem(Sta te As Object)
Dim WorkerAs Worker= CType(State,Wor ker)
Try
'process item
Worker.Process( )
Catch ex As Exception
'return item to queue
Queue.Enqueue(W orker.Item)
Finally
'return worker to pool
ObjectPool.retu rnWorker(Worker )
End Try
End Sub
"Craig Buchanan" <so*****@somewh ere.comwrote:
is this your blog: http://instructors.cwrl.utexas.edu/jesson/?q=blog/61 ?
Nope, not my blog. I'll give ya a hint though: look at the bottom of this
message. The owner of that blog would probably cry if he was forced to ready
my poor writing all day long...
I'll post the ObjectPool<Tcod e to my blog in a few days (when I write my
next entry).
Ping me if I forget...
--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP http://www.coversant.com/blogs/cmullins
Thanks for the help.
Craig
"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:uK******** ********@TK2MSF TNGP03.phx.gbl. ..
"Craig Buchanan" <so*****@somewh ere.comwrote:
>is this your blog: http://instructors.cwrl.utexas.edu/jesson/?q=blog/61 ?
Nope, not my blog. I'll give ya a hint though: look at the bottom of this
message. The owner of that blog would probably cry if he was forced to
ready my poor writing all day long...
I'll post the ObjectPool<Tcod e to my blog in a few days (when I write my
next entry).
Ping me if I forget...
--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP http://www.coversant.com/blogs/cmullins
Brian-
That's pretty slick. It makes a lot of sense too.
Here's an example of a blocking queue: http://www.codeproject.com/csharp/bo...ckingqueue.asp
Thanks for the reply,
Craig
"Brian Gideon" <br*********@ya hoo.comwrote in message
news:11******** *************@y 80g2000hsf.goog legroups.com...
On Mar 27, 3:37 pm, "Craig Buchanan" <some...@somewh ere.comwrote:
>I would like to have a limited pool of objects (the objects are expensive to create) that can be enlisted to process items in a queue. Moreover, I would like to be able to have the processing (time-consuming) occur on a background thread.
I have two questions: * what is a good way to block the queue loop while it waits for a free worker?
The best way to block the loop is to block the getFreeWorker method
until a free worker is available. The pattern you're looking for is a
producer-consumer queue or blocking queue. Look for the
ProducerConsume r class in the following article. The Produce and
Consume methods in that class would map directly to your returnWorker
and getFreeWorker methods respectively. I know the examples in the
article are in C#, but you'll get the idea.
http://www.yoda.arachsys.com/csharp/...eadlocks.shtml
>* is my approach a reasonable one?
Yes, I think it is reasonable. There might be some minor changes I
would change regarding your general pattern, but nothing worth
mentioning.
>Thanks,
Craig Buchanan
Here's my psedo-code:
Sub ProcessQueue
Do While Queue.Count 0
'wait for next available worker Worker = ObjectPool.getF reeWorker()
'assign next item in the queue to the worker Worker.Item = Queue.Dequeue
'run time-consuming process in background ThreadPool.Queu eUserWorkItem(A ddressOf ProcessItem, Worker)
Loop
End Sub
Sub ProcessItem(Sta te As Object)
Dim WorkerAs Worker= CType(State,Wor ker)
Try
'process item Worker.Process( )
Catch ex As Exception
'return item to queue Queue.Enqueue(W orker.Item)
Finally
'return worker to pool ObjectPool.retu rnWorker(Worker )
End Try
End Sub
Chris-
Are you still planning to add this to your blog?
Thanks,
Craig
"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:uK******** ********@TK2MSF TNGP03.phx.gbl. ..
"Craig Buchanan" <so*****@somewh ere.comwrote:
>is this your blog: http://instructors.cwrl.utexas.edu/jesson/?q=blog/61 ?
Nope, not my blog. I'll give ya a hint though: look at the bottom of this
message. The owner of that blog would probably cry if he was forced to
ready my poor writing all day long...
I'll post the ObjectPool<Tcod e to my blog in a few days (when I write my
next entry).
Ping me if I forget...
--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP http://www.coversant.com/blogs/cmullins
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: phil |
last post by:
And sorry I got ticked, frustrating week
>And I could help more, being fairly experienced with
>threading issues and race conditions and such, but
>as I tried to indicate in the first place, you've
>provided next to no useful (IMHO) information to
>let anyone help you more than this
This is about 5% of the code.
Uses no locks.
|
by: Brian Henry |
last post by:
If i inherite a queue class into my class, and do an override of the enqueue
member, how would i then go about actually doing an enqueue of an item? I am
a little confused on this one... does over ride just add aditional code
ontop of the current class or completely over ride it in vb? I am use to C++
this is the first inherited thing I've done in VB.NET... I'm a little unsure
of diffrences, could someone explain this to me some? thanks!
|
by: Kceiw |
last post by:
Dear all,
When I use #include "queue.h", I can't link it.
The error message follows:
Linking...
G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In
function `main':
G:\Projects\Datastructure\Queue\main.cpp:16: undefined reference to
`Queue<char>::Queue()'
G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x394): In
function `Z10do_commandcR5QueueIcE':
|
by: Rooro |
last post by:
Hello everyone
i'm working on :
"
Familiar childhood games such as hide and Go Seek and Tag involve determining the player who is to be "It". One method has the players stand in a circle while one of the players recites some rhyme to count off players, eliminating every nth player, where n depends on the number of the syllables in the counting rhyme. This process stops when only one player, the one to be "It" remains.
This process...
|
by: lavender |
last post by:
When define a maxQueue is 10, means it able to store 10 items in circular queue,but when I key in the 10 items, it show "Queue Full" in items number 10.
Where is the wrong in my code? Why it cannot store up to 10 items?
Output from my code:
Enter you choice: 1
Enter ID document to print : 21
Enter you choice: 1
Enter ID document to print : 22
| |
by: jrpfinch |
last post by:
I have a script which is based on the following code. Unfortunately,
it only works on Python 2.3 and not 2.5 because there is no esema or
fsema attribute in the 2.5 Queue. I am hunting through the Queue.py
code now to try to figure out how to make it work in 2.5, but as I am
a beginner, I am having difficulty and would appreciate your help.
Many thanks
Jon
|
by: j_depp_99 |
last post by:
Thanks to those guys who helped me out yesterday. I have one more
problem; my print function for the queue program doesnt work and goes
into an endless loop. Also I am unable to calculate the length of my
queue. I started getting compilation errors when I included a length
function.
<code>
template<class ItemType>
void Queue<ItemType>::MakeEmpty()
{
|
by: ecestd |
last post by:
how do you implement a copy constructor for this pointer-based ADT
queue
#include <cassert // for assert
#include <new // for bad_alloc
using namespace std;
//private:{Queue::Queue(const Queue& Q)}
|
by: ecestd |
last post by:
I did implement the copy constructor but still have a problem with
it. It is not working. What could be wrong?
#include "QueueP.h"
#include <cassert // for assert
#include <new // for bad_alloc
#include <iostream>
//typedef std::queue<QueueItemTypeQueue;
using namespace std;
//private:{Queue::Queue(const Queue& Q)}
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |