473,397 Members | 2,077 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,397 software developers and data experts.

ReaderWriterLock and Collections

Question:

Is this thread-safe:

ReaderWriterLock rwl = new ReaderWriterLock();
Queue q = new Queue();

public int GetCount() {
int val = 0;
try {
rwl.AcquireReaderLock(Timeout.Infinite);
val = q.Count;
} finally {
rwl.ReleaseReaderLock();
}
return val;
}

Or do I need to do:

public int GetCount() {
int val = 0;
try {
rwl.AcquireReaderLock(Timeout.Infinite);
lock(q.SyncRoot) {
val = q.Count;
}
} finally {
rwl.ReleaseReaderLock();
}
return val;
}
Nov 15 '05 #1
2 3275
I think that you're looking for this;

Queue q = Queue.Synchronized( new Queue() );

Otherwise,

If you always acquire some lock on the ReaderWriterLock, there is no need
need to "lock ( q.SyncRoot )", although you'd you'd probably want to make a
queue wrapper class to ensure that this was enforced.
"pokémon" <po**@mon.com> wrote in message
news:SK************@nwrdny02.gnilink.net...
Question:

Is this thread-safe:

ReaderWriterLock rwl = new ReaderWriterLock();
Queue q = new Queue();

public int GetCount() {
int val = 0;
try {
rwl.AcquireReaderLock(Timeout.Infinite);
val = q.Count;
} finally {
rwl.ReleaseReaderLock();
}
return val;
}

Or do I need to do:

public int GetCount() {
int val = 0;
try {
rwl.AcquireReaderLock(Timeout.Infinite);
lock(q.SyncRoot) {
val = q.Count;
}
} finally {
rwl.ReleaseReaderLock();
}
return val;
}

Nov 15 '05 #2
Yes, the wrapper is doing the locking, and the Queue object itself is
private and thus hidden from the outsiders that use it.

For some reason, I have shied away from Queue.Synchronized. I can't explain
why, only that it seems less literal to use it for synchronization, than
actually coding the extra lines which make it clear to another developer
what the deal is.

Also, isn't the RWL slightly faster than using the Synchronized wrapper?
"Brad Quinn" <br********@yahoo.com> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl...
I think that you're looking for this;

Queue q = Queue.Synchronized( new Queue() );

Otherwise,

If you always acquire some lock on the ReaderWriterLock, there is no need
need to "lock ( q.SyncRoot )", although you'd you'd probably want to make a queue wrapper class to ensure that this was enforced.
"pokémon" <po**@mon.com> wrote in message
news:SK************@nwrdny02.gnilink.net...
Question:

Is this thread-safe:

ReaderWriterLock rwl = new ReaderWriterLock();
Queue q = new Queue();

public int GetCount() {
int val = 0;
try {
rwl.AcquireReaderLock(Timeout.Infinite);
val = q.Count;
} finally {
rwl.ReleaseReaderLock();
}
return val;
}

Or do I need to do:

public int GetCount() {
int val = 0;
try {
rwl.AcquireReaderLock(Timeout.Infinite);
lock(q.SyncRoot) {
val = q.Count;
}
} finally {
rwl.ReleaseReaderLock();
}
return val;
}


Nov 15 '05 #3

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

Similar topics

2
by: Tryion | last post by:
Hi, I'd like to know if it's possible/responsible to use the ReaderWriterLock class (RWL) in a class without declaring it as "static". The example in the SDK does not use a static RWL. However,...
3
by: J.Marsch | last post by:
Issue: I have 3 threads, syncing with a ReaderWriterLock (in "real life", there will be more). Thread 1 (there could be any number of these) Gets a read lock with infinite timeout. It runs in...
0
by: Qingdong Z. | last post by:
I have a public shared DateTime type variable called . There are multiple work threads updating this variable to current time (Now). Normally, the DateTime variable is updated 10-100 times per...
6
by: Itay | last post by:
Hi , Is there a way to lock several ReaderWriterLock in an atomic manner? I have numerous amount of ReaderWriterLock objects and I need to lock some of them for reading only, is there a way...
4
by: Anders Borum | last post by:
Hello! I am working on improving my threading skills and came across a question. When working with the ReaderWriterLock class, I am getting an unhandled exception if I acquire a WriterLock with...
7
by: Julie | last post by:
According to the documentation for the Acquire methods on the ReaderWriterLock class: -1 Infinite. 0 No time-out. > 0 The number of milliseconds to wait. ...
1
by: fm | last post by:
I have a public shared property in global.asax. When the variable of the property is not instantiated (first page to call it, cache empties, etc.) the code then loads the variable from source...
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...
4
by: Sid Price | last post by:
Hello, I have a class of objects (Device) that are managed by another object (Devices) with a collection class (DeviceCollection) inherited from Collections.Hashtable. Each of the Device objects...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.