470,647 Members | 1,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,647 developers. It's quick & easy.

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 3177
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Qingdong Z. | last post: by
6 posts views Thread by Itay | last post: by
4 posts views Thread by Adam Clauss | last post: by
4 posts views Thread by Sid Price | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.