473,230 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

System.Collections.Generic.Queue & System.Collections.Queue Synchronized Method

Hi,

Does System.Collections.Generic.Queue not have a Synchronized
method because it is already in effect synchronized, or is the
Synchronized functionality missing from
System.Collections.Generic.Queue? Putting it another way can I
safely replace a
System.Collections.Queue.Synchronized(myUnSynchron izedQueue) with a
System.Collections.Generic.Queue while porting a working 2003 project?
Thanks,
Mark
Feb 15 '06 #1
4 8148
The Queue<T> is not thread safe.
"Public static (Shared in Visual Basic) members of this type are thread
safe. Any instance members are not guaranteed to be thread safe.

A Queue can support multiple readers concurrently, as long as the collection
is not modified. Even so, enumerating through a collection is intrinsically
not a thread-safe procedure. To guarantee thread safety during enumeration,
you can lock the collection during the entire enumeration. To allow the
collection to be accessed by multiple threads for reading and writing, you
must implement your own synchronization."

I would provide your own syncRoot and lock on that during instance method
calls.
--
William Stacey [MVP]
<nh******@newsgroup.nospam> wrote in message
news:4r********************************@4ax.com...
| Hi,
|
| Does System.Collections.Generic.Queue not have a Synchronized
| method because it is already in effect synchronized, or is the
| Synchronized functionality missing from
| System.Collections.Generic.Queue? Putting it another way can I
| safely replace a
| System.Collections.Queue.Synchronized(myUnSynchron izedQueue) with a
| System.Collections.Generic.Queue while porting a working 2003 project?
| Thanks,
| Mark
Feb 15 '06 #2
Hi,

Thanks for your help.

I can safely replace 2003 C#

static System.Collections.Queue myUnSynchronizedQueue=new
System.Collections.Queue();
static System.Collections.Queue
mySynchronizedQueue=System.Collections.Queue.Synch ronized(myUnSynchronizedQueue);
void f() {
lock(mySynchronizedQueue.SyncRoot)
{
IEnumerator myEnumerator=mySynchronizedQueue.GetEnumerator();
}
}

with 2005 C#

static System.Collections.Generic.Queue<myType>
mymSynchronizedQueue=new System.Collections.Generic.Queue<myType>();

void f() {

lock(mySynchronizedQueue)
{
IEnumerator<myType>
myEnumerator=mySynchronizedQueue.GetEnumerator();
}
}

without any other code modifications (except removing unnecessary
type casts) while porting a working 2003 project?

Thanks,
Mark
On Wed, 15 Feb 2006 03:42:24 -0500, "William Stacey [MVP]"
<wi************@gmail.com> wrote:
The Queue<T> is not thread safe.
"Public static (Shared in Visual Basic) members of this type are thread
safe. Any instance members are not guaranteed to be thread safe.

A Queue can support multiple readers concurrently, as long as the collection
is not modified. Even so, enumerating through a collection is intrinsically
not a thread-safe procedure. To guarantee thread safety during enumeration,
you can lock the collection during the entire enumeration. To allow the
collection to be accessed by multiple threads for reading and writing, you
must implement your own synchronization."

I would provide your own syncRoot and lock on that during instance method
calls.

Feb 16 '06 #3
nh******@newsgroup.nospam wrote:
I can safely replace 2003 C#

static System.Collections.Queue myUnSynchronizedQueue=new
System.Collections.Queue();
static System.Collections.Queue
mySynchronizedQueue=System.Collections.Queue.Synch ronized(myUnSynchronizedQueue);
void f() {
lock(mySynchronizedQueue.SyncRoot)
{
IEnumerator myEnumerator=mySynchronizedQueue.GetEnumerator();
}
}

with 2005 C#

static System.Collections.Generic.Queue<myType>
mymSynchronizedQueue=new System.Collections.Generic.Queue<myType>();

void f() {

lock(mySynchronizedQueue)
{
IEnumerator<myType>
myEnumerator=mySynchronizedQueue.GetEnumerator();
}
}

without any other code modifications (except removing unnecessary
type casts) while porting a working 2003 project?


Neither of those will give you thread-safe enumeration. You need to
hold the lock throughout the duration of the enumeration - not just for
the call to GetEnumerator().

If the above is called, and then another thread modifies the collection
before the enumerator is actually used (or during its use) you'll get
an exception when you use the enumerator.

Jon

Feb 16 '06 #4
Thanks,
Mark

On 15 Feb 2006 23:58:27 -0800, "Jon Skeet [C# MVP]" <sk***@pobox.com>
wrote:
nh******@newsgroup.nospam wrote:
I can safely replace 2003 C#

static System.Collections.Queue myUnSynchronizedQueue=new
System.Collections.Queue();
static System.Collections.Queue
mySynchronizedQueue=System.Collections.Queue.Synch ronized(myUnSynchronizedQueue);
void f() {
lock(mySynchronizedQueue.SyncRoot)
{
IEnumerator myEnumerator=mySynchronizedQueue.GetEnumerator();
}
}

with 2005 C#

static System.Collections.Generic.Queue<myType>
mymSynchronizedQueue=new System.Collections.Generic.Queue<myType>();

void f() {

lock(mySynchronizedQueue)
{
IEnumerator<myType>
myEnumerator=mySynchronizedQueue.GetEnumerator();
}
}

without any other code modifications (except removing unnecessary
type casts) while porting a working 2003 project?


Neither of those will give you thread-safe enumeration. You need to
hold the lock throughout the duration of the enumeration - not just for
the call to GetEnumerator().

If the above is called, and then another thread modifies the collection
before the enumerator is actually used (or during its use) you'll get
an exception when you use the enumerator.

Jon

Feb 18 '06 #5

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

Similar topics

1
by: Helium | last post by:
Is there any need for "System.Collections.Queue" any more? OK, .Net made the mistake to start without generics, but that is fixed now. Languages without support for generics could use...
2
by: pokémon | last post by:
Question: Is this thread-safe: ReaderWriterLock rwl = new ReaderWriterLock(); Queue q = new Queue(); public int GetCount() { int val = 0; try {
6
by: rmunson8 | last post by:
I have a derived class from the Queue base class. I need it to be thread-safe, so I am using the Synchronized method (among other things out of scope of this issue). The code below compiles, but...
2
by: Howard Swope | last post by:
Could someone help explain thread safety issues in the System.Collections classes? The documentation states:...
22
by: Adam Clauss | last post by:
OK, I have class A defined as follows: class A { A(Queue<B> queue) { ... } } Now, I then have a subclass of both classes A and B. The subclass of A (SubA), more specifically is passed a...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
6
by: Mark Rae | last post by:
Hi, I'm in the process of updating an ASP.NET v1.1 web app to v2. The app uses ActiveDirectory a great deal, and I'm trying to use the new System.Collections.Generic namespace where possible,...
2
by: =?Utf-8?B?TWluIFlvbmc=?= | last post by:
I'm not sure if I've just lost it but, I no longer see the property SyncRoot on a Queue. I see that it's a member of the ICollection Base Object, but it's not exposed. Was this intended? Besides...
2
by: Fred Heida | last post by:
Hi, i'm trying to (using managed C++) implment the IEnumerable<Tinterface on my class.. but have a problem with the 2 GetEnumerator method required.... what i have done is... ...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.