471,325 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 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 7971
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by pokémon | last post: by
6 posts views Thread by rmunson8 | last post: by
2 posts views Thread by Howard Swope | last post: by
22 posts views Thread by Adam Clauss | last post: by
4 posts views Thread by Adam Clauss | last post: by
2 posts views Thread by =?Utf-8?B?TWluIFlvbmc=?= | last post: by
reply views Thread by rosydwin | last post: by

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.