473,698 Members | 2,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing thread safe Queue collection object

Hi,

I've been reading the online documentation for the System.Collecti ons.Queue
object on how to implement a threadsafe Queue object.

I understand the basics, by using the wrapper returned by Synchronised
method, but I'm not sure how to implement it between 2 threads. Has anyone
got any suggestions or examples?

Thanks
Macca
Jan 30 '06 #1
4 17337
When you say "betweeen 2 threads" I'm not sure I understand the intent. The
samples online are very simple:

Queue myCollection = new Queue();
lock( myCollection.Sy ncRoot ) {
foreach ( Object item in myCollection ) {
// Insert your code here.
}
}

Until the closing brace of the lock statement, no other thread can operate
on the Queue.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Macca" wrote:
Hi,

I've been reading the online documentation for the System.Collecti ons.Queue
object on how to implement a threadsafe Queue object.

I understand the basics, by using the wrapper returned by Synchronised
method, but I'm not sure how to implement it between 2 threads. Has anyone
got any suggestions or examples?

Thanks
Macca

Jan 30 '06 #2

"Macca" <Ma***@discussi ons.microsoft.c om> wrote in message
news:C7******** *************** ***********@mic rosoft.com...
Hi,

I've been reading the online documentation for the
System.Collecti ons.Queue
object on how to implement a threadsafe Queue object.

I understand the basics, by using the wrapper returned by Synchronised
method, but I'm not sure how to implement it between 2 threads. Has anyone
got any suggestions or examples?

Thanks
Macca


If you mean to send stuff from one thread to another then you need to write
your own wrapper and use
Monitor.Wait(q) to wait for the Q to become non-empty and
Monitor.PulseAl l(q) when you put something on it.

i.e. something like:

class MyQ
{
private Queue q;

public void Enqueue(MyClass x)
{
lock(q)
{
q.Enqueue(x);
Monitor.PulseAl l(q);
}
}

public MyClass Dequeue(MyClass x)
{
lock(q)
{
while(q.Empty)
Monitor.Wait(q) ;
return (MyClass)q.Dequ eue();
}
}
}

Note that Synchronized is not used as the Queue is not exposed and locking
is handled in the methods.
Jan 31 '06 #3
Thanks for the reply Peter.

What I mean is that I have one thread that adds entries to the queue while
another takes them off and processes them. I'm assuming the queue will be a
shared item between the 2 threads although not sure exactly where is best
place to store it. In first thread?

While items are being processed it should be possible to add entries to the
queue at the same time although if processing thread is fast enough this may
not be a requirement.

I assume the processing thread will have to poll the queue to see if there
is anything in the queue first although it would be nice to have this event
driven rather than having to poll.

I would have thought this is not an uncommon scenario, one thread building
up the queue while another extracts entries and processes them?

What I dont want is the processing of the entries to block entries being put
on the queue therefore the need for two threads.

What do you think?

Regards
Macca

"Peter Bromberg [C# MVP]" wrote:
When you say "betweeen 2 threads" I'm not sure I understand the intent. The
samples online are very simple:

Queue myCollection = new Queue();
lock( myCollection.Sy ncRoot ) {
foreach ( Object item in myCollection ) {
// Insert your code here.
}
}

Until the closing brace of the lock statement, no other thread can operate
on the Queue.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Macca" wrote:
Hi,

I've been reading the online documentation for the System.Collecti ons.Queue
object on how to implement a threadsafe Queue object.

I understand the basics, by using the wrapper returned by Synchronised
method, but I'm not sure how to implement it between 2 threads. Has anyone
got any suggestions or examples?

Thanks
Macca

Jan 31 '06 #4
Macca wrote:
Thanks for the reply Peter.

What I mean is that I have one thread that adds entries to the queue while
another takes them off and processes them. I'm assuming the queue will be a
shared item between the 2 threads although not sure exactly where is best
place to store it. In first thread?
You don't "store" items in threads.

It sounds like you really want a producer/consumer queue. See half way
down
http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml for some
sample code.
While items are being processed it should be possible to add entries to the
queue at the same time although if processing thread is fast enough this may
not be a requirement.

I assume the processing thread will have to poll the queue to see if there
is anything in the queue first although it would be nice to have this event
driven rather than having to poll.


The version I've got on the page referenced above blocks on the call to
Consume until a work item is ready. You'd therefore end up with a
thread which did nothing but consume work items. You should be able to
modify the code appropriately if you want though.

Jon

Jan 31 '06 #5

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

Similar topics

7
7867
by: sayoyo | last post by:
Hi, Is there some way that we can reuse a thread by replacing the runnable object of the thread? like a thread is not "alive" anymore, then we remove the runnable object(is it possible????) and then run it again. Thanks you very much Sayoyo
3
17785
by: Philip V Pham | last post by:
These questions apply to std vector, map, and cout: I am uncertain of the thread safety for reading/writing for std templates. I know if all threads are reading concurrently, it is thread safe. However, I have this situation: Case 1: map thread 1 ---------
6
3199
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
0
1617
by: Dave Coate | last post by:
I am working on a generic way to launch multiple similar processes (threads) at once, but limit the number of threads running at any one time to a number I set. As I understand it the following line makes a Queue "thread safe", so I do not need to explicitly lock and unlock it when multiple threads are working with it. 'Thread safe queue Private IndexQueue As Queue = Queue.Synchronized(New Queue)
4
3328
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
4
10276
by: Russell Warren | last post by:
I'm guessing no, since it skips down through any Lock semantics, but I'm wondering what the best way to clear a Queue is then. Esentially I want to do a "get all" and ignore what pops out, but I don't want to loop through a .get until empty because that could potentially end up racing another thread that is more-or-less blindly filling it asynchronously. Worst case I think I can just borrow the locking logic from Queue.get and clear...
6
3137
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it to the client. Before adding data to the arraylist, I check if the depth of the arraylist is longer than iMaxQueueDepth, and if it is, I clear the arraylist. Is it possible that while I am clearing the arraylist, the ThreadMain at the same time...
6
2114
by: cyberco | last post by:
In my wxPython app a non-GUI thread (that reads info from the network) tries to open a frame to show the new info. This results in my app hanging (which is not too surprising). Coming from a C# environment I wonder if there is some sort of delegate mechanism in wxPython to do this sort of thing. 2B
29
9122
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: --Thread 1-- 1. Reads TCPIP Buffer 2. Adds Buffer to Queue (q.size() to check queue isn't full, and q.push_back(...)) 3. Signals Reading Thread Event & Goes Back to Wait for More Messages on TCPIP
0
8603
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,...
0
9157
Oralloy
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...
0
9027
jinu1996
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...
1
8895
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,...
0
7725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 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...
0
4369
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...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.