473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Collecti ons.Generic.Que ue & System.Collecti ons.Queue Synchronized Method

Hi,

Does System.Collecti ons.Generic.Que ue not have a Synchronized
method because it is already in effect synchronized, or is the
Synchronized functionality missing from
System.Collecti ons.Generic.Que ue? Putting it another way can I
safely replace a
System.Collecti ons.Queue.Synch ronized(myUnSyn chronizedQueue) with a
System.Collecti ons.Generic.Que ue while porting a working 2003 project?
Thanks,
Mark
Feb 15 '06 #1
4 8177
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******@newsg roup.nospam> wrote in message
news:4r******** *************** *********@4ax.c om...
| Hi,
|
| Does System.Collecti ons.Generic.Que ue not have a Synchronized
| method because it is already in effect synchronized, or is the
| Synchronized functionality missing from
| System.Collecti ons.Generic.Que ue? Putting it another way can I
| safely replace a
| System.Collecti ons.Queue.Synch ronized(myUnSyn chronizedQueue) with a
| System.Collecti ons.Generic.Que ue 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.Collecti ons.Queue myUnSynchronize dQueue=new
System.Collecti ons.Queue();
static System.Collecti ons.Queue
mySynchronizedQ ueue=System.Col lections.Queue. Synchronized(my UnSynchronizedQ ueue);
void f() {
lock(mySynchron izedQueue.SyncR oot)
{
IEnumerator myEnumerator=my SynchronizedQue ue.GetEnumerato r();
}
}

with 2005 C#

static System.Collecti ons.Generic.Que ue<myType>
mymSynchronized Queue=new System.Collecti ons.Generic.Que ue<myType>();

void f() {

lock(mySynchron izedQueue)
{
IEnumerator<myT ype>
myEnumerator=my SynchronizedQue ue.GetEnumerato r();
}
}

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******@newsgr oup.nospam wrote:
I can safely replace 2003 C#

static System.Collecti ons.Queue myUnSynchronize dQueue=new
System.Collecti ons.Queue();
static System.Collecti ons.Queue
mySynchronizedQ ueue=System.Col lections.Queue. Synchronized(my UnSynchronizedQ ueue);
void f() {
lock(mySynchron izedQueue.SyncR oot)
{
IEnumerator myEnumerator=my SynchronizedQue ue.GetEnumerato r();
}
}

with 2005 C#

static System.Collecti ons.Generic.Que ue<myType>
mymSynchronized Queue=new System.Collecti ons.Generic.Que ue<myType>();

void f() {

lock(mySynchron izedQueue)
{
IEnumerator<myT ype>
myEnumerator=my SynchronizedQue ue.GetEnumerato r();
}
}

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.co m>
wrote:
nh******@newsg roup.nospam wrote:
I can safely replace 2003 C#

static System.Collecti ons.Queue myUnSynchronize dQueue=new
System.Collecti ons.Queue();
static System.Collecti ons.Queue
mySynchronizedQ ueue=System.Col lections.Queue. Synchronized(my UnSynchronizedQ ueue);
void f() {
lock(mySynchron izedQueue.SyncR oot)
{
IEnumerator myEnumerator=my SynchronizedQue ue.GetEnumerato r();
}
}

with 2005 C#

static System.Collecti ons.Generic.Que ue<myType>
mymSynchronized Queue=new System.Collecti ons.Generic.Que ue<myType>();

void f() {

lock(mySynchron izedQueue)
{
IEnumerator<myT ype>
myEnumerator=my SynchronizedQue ue.GetEnumerato r();
}
}

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
420
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 "System.Collections.Generics.Queue<Object>" instead of "System.Collections.Queue". So everytime those languages use a generic, they just use them with <object>. I don't think having two different sets of collections is a good idea.
2
3290
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
3720
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 at runtime, the cast of "Queue.Synchronized(new EventQueue())" to an EventQueue object is failing stating invalid cast. Why? public class EventQueue : System.Collections.Queue { ... }
2
2886
by: Howard Swope | last post by:
Could someone help explain thread safety issues in the System.Collections classes? The documentation states: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Thread Safety Public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. A SortedList can support multiple readers concurrently,...
22
13100
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 Queue<SubB>.
4
2214
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 the old code allowed me to do what basically was the following assignment: class SomeClass { private Queue q; SomeClass(Queue q)
6
4073
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, having been advised by several luminaries that that is a "good thing to do"... :-) However, I'm experiencing a problem with the IEnumerable interface. (N.B. I understand fully that I should be using the LDAP provider instead of the WinNT provider...
2
6094
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 writing a wrapper class for Queue, what is the intended way to lock the queue to enforce proper synchronization? using System; using System.Collections.Generic; using System.Text;
2
7351
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... generic<typename T> public ref class SetOfProxy : public System::Collections::Generic::IEnumerable<T>
0
8009
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8432
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
8428
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...
0
8299
tracyyun
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...
1
5962
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
3919
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...
1
2442
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1548
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.