472,958 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 3254
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.