473,396 Members | 1,945 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,396 software developers and data experts.

ReaderWriterLock

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 doing it one shot ?

Itay.
Nov 16 '05 #1
6 2137
Hi Itay:

I don't know a way to do it in one shot. Can you guarantee the code
will always acquire locks in the same order?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 7 Nov 2004 07:45:50 -0800, it***@nur.com (Itay) wrote:
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 doing it one shot ?

Itay.


Nov 16 '05 #2
Scott Allen <bitmask@[nospam].fred.net> wrote in message news:<2i********************************@4ax.com>. ..
Hi Itay:

I don't know a way to do it in one shot. Can you guarantee the code
will always acquire locks in the same order?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 7 Nov 2004 07:45:50 -0800, it***@nur.com (Itay) wrote:
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 doing it one shot ?

Itay.

Suppose i can , how can it be helpfull ?
Do you suggest looping over the lockers ? doesn't it invites a deadlock ?
Nov 16 '05 #3
Hi Itay:

In general, if you have multiple locks to acquire then as long as
everyone acquires them in the same order you can avoid deadlock.

For example, let's say I have multiple locks to acquire: locks A, B,
and C. I'd establish a rule that says no code can acquire lock C
without first acquiring lock B, and no code can acquire lock B without
first acquiring lock A. This prevents the scenario where a thread has
lock B and needs lock A, and another thread has lock A and needs lock
B.

If you have a collection of locks you can loop through them and
acquire each in turn. As long as all the code follows the same
protocol by looping in the same order, acquiring each in turn, there
will be no problems.

The correct locking order is often referred to as a 'lock hierarchy'
in MT circles. The classic trade off is that you are probably trying
to implement a fine grained locking mechanism, and a lock hierarchy
invariably pushes the design back to doing more coarse grained
locking, so it's a very delicate balance to find. It's usually best to
start with a simple coarse grained locking mechanism, then measure
performance and move to a more complicated solution only if necessity
requires it.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 7 Nov 2004 23:14:19 -0800, it***@nur.com (Itay) wrote:
Scott Allen <bitmask@[nospam].fred.net> wrote in message news:<2i********************************@4ax.com>. ..
Hi Itay:

I don't know a way to do it in one shot. Can you guarantee the code
will always acquire locks in the same order?

--
Scott
http://www.OdeToCode.com/blogs/scott/

On 7 Nov 2004 07:45:50 -0800, it***@nur.com (Itay) wrote:
>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 doing it one shot ?
>
>Itay.

Suppose i can , how can it be helpfull ?
Do you suggest looping over the lockers ? doesn't it invites a deadlock ?


Nov 16 '05 #4
I would be interested if anyone knows, what kind of synchronization
mechanisms are used for distributed computing using .Net.

For instance, User level vs. Kernel level on distributed computers. I
read about something called Test & Set. Is that used by .Net?

Thanks.
Nov 16 '05 #5
> The classic trade off is that you are probably trying
to implement a fine grained locking mechanism, and a lock hierarchy
invariably pushes the design back to doing more coarse grained
locking, so it's a very delicate balance to find. It's usually best to
start with a simple coarse grained locking mechanism, then measure
performance and move to a more complicated solution only if necessity
requires it.


So true Scott. You can get bound up in a nest of locks and your
thread-lock-bug matrix expands like my stomach at Thanksgiving. I went down
the fine grain lock for a memory based object db thing I was doing and went
back and forth a couple times. In the end, I found one lock was better then
anything else I could come up with. Especially, when you concider other
"Users" for your DB like WebServices, etc. Some API may want to iterate
over your collection(s), some update, and some read. Also remember that RW
locks are a lot slower then monitors, so you need to factor in the
cost/benefits of that. You may be able to service 3+ Monitor requests for
every one (need to test) RW lock and add multiple RW or monitor locks and
things get a bit slower still. Also, the more locks you need to
grab...well, the more locks you need to grab - and they are not free. So
fewer towards one "may" be better depending. Also, you may want to look at
a larger thread picture. How many threads do you actually need? Are you
sure you need that many? Is it making your app more responsive (i.e. more
requests per second) or slower? How much blocking is going on inside the
APIs? Maybe only one thread needs to touch the db at any one time and all
requests are serviced from a blocking circular queue so no lock may even be
required (cq will still need one lock) - this method has a lot to love about
it - but depends on your design and needs. Interesting and complex topic.
Cheers.

--
William Stacey, MVP
http://mvp.support.microsoft.com
Nov 16 '05 #6
> I would be interested if anyone knows, what kind of synchronization
mechanisms are used for distributed computing using .Net.
The normal lock (i.e. Monitor), rw locks, events, mutexs are all used. The
native win32 mutex, sem, events, etc can still be used but I would typically
not use them unless I needed the "named" versions for cross process
something or other (and I think fw 2.0 has managed wrapper for them now).
For instance, User level vs. Kernel level on distributed computers. I
read about something called Test & Set. Is that used by .Net?


This basically translates to the Interlocked.CompareExchange (i.e.
Interlocked.CompareExchange, Interlocked.Increment, etc.) The Increment and
Decrement methods are most useful. CompareExchange can be handy if you have
special needs or require very special attention to performance, but more
folks try to stay away from that in favor of standard Monitor symatics. A
major reason is it is easier to "reason" about and prove out the correctness
of your code using locks then using low level Interlocked apis. However, if
you need them, its great to have them. hth.

--
William Stacey, MVP
http://mvp.support.microsoft.com
Nov 16 '05 #7

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

Similar topics

2
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 {
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...
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...
3
by: Lore Leunoeg | last post by:
Is there any difference between: - ReleaseLock and RestoreLock and - simply releasing the Lock with ReleaseReaderLock or ReleaseWriterLockand acquire the lock again with AcquireReaderLock or...
16
by: akantrowitz | last post by:
In csharp, what is the correct locking around reading and writing into a hashtable. Note that the reader is not looping through the keys, simply reading an item out with a specific key: If i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.