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

  • Thread starter Thread starter nhmark64
  • Start date Start date
N

nhmark64

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(myUnSynchronizedQueue) with a
System.Collections.Generic.Queue while porting a working 2003 project?
Thanks,
Mark
 
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]


| 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(myUnSynchronizedQueue) with a
| System.Collections.Generic.Queue while porting a working 2003 project?
| Thanks,
| Mark
 
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.Synchronized(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
 
I can safely replace 2003 C#

static System.Collections.Queue myUnSynchronizedQueue=new
System.Collections.Queue();
static System.Collections.Queue
mySynchronizedQueue=System.Collections.Queue.Synchronized(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
 
Thanks,
Mark



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
 
Back
Top