ReaderWriterLock
-
Hi everyone! I wanted to know how the ReaderWriterLock determines what is the "resource" in the code on which it has to accquire the reader lock or the writer lock, or how exactly does these locks work(internally). The MSDN article protects an static integer, but doesn't seem to specify how, as after acquiring a reader lock, writes can be done as in the following code:
ReaderWriterLock rw = new ReaderWriterLock();
int counter = 0;
try
{
rw.AcquireReaderLock(100);
try
{
Interlocked.Increment(ref counter);
}
finally
{
rw.ReleaseLock();
}
}
catch (ApplicationException)
{
Console.WriteLine("Failed");
}
Console.WriteLine(counter);here we are acquiring an reader lock for 100ms, but writing to the data. Confused :doh:
-
Hi everyone! I wanted to know how the ReaderWriterLock determines what is the "resource" in the code on which it has to accquire the reader lock or the writer lock, or how exactly does these locks work(internally). The MSDN article protects an static integer, but doesn't seem to specify how, as after acquiring a reader lock, writes can be done as in the following code:
ReaderWriterLock rw = new ReaderWriterLock();
int counter = 0;
try
{
rw.AcquireReaderLock(100);
try
{
Interlocked.Increment(ref counter);
}
finally
{
rw.ReleaseLock();
}
}
catch (ApplicationException)
{
Console.WriteLine("Failed");
}
Console.WriteLine(counter);here we are acquiring an reader lock for 100ms, but writing to the data. Confused :doh:
Hi, a ReaderWriterLock, just like all other synchronization tools, is just a tool; you must use it correctly in order to achieve your goals. It does not protect you, or any of your objects, against programming mistakes. For the RWL that means you must use it where ever you access the object you want protected against multi-threaded accesses, and you must be careful about calling the right method; it won't help much calling AcquireReaderLock() when you intend to write to the variable you are trying to protect. :)
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text (not TextBoxes), and PictureBoxes for pictures (not drawings).
-
Hi everyone! I wanted to know how the ReaderWriterLock determines what is the "resource" in the code on which it has to accquire the reader lock or the writer lock, or how exactly does these locks work(internally). The MSDN article protects an static integer, but doesn't seem to specify how, as after acquiring a reader lock, writes can be done as in the following code:
ReaderWriterLock rw = new ReaderWriterLock();
int counter = 0;
try
{
rw.AcquireReaderLock(100);
try
{
Interlocked.Increment(ref counter);
}
finally
{
rw.ReleaseLock();
}
}
catch (ApplicationException)
{
Console.WriteLine("Failed");
}
Console.WriteLine(counter);here we are acquiring an reader lock for 100ms, but writing to the data. Confused :doh:
Your blending two different kinds of locking. If you use Interlocked, all operations performed with it ARE thread-safe...you don't need to bother with a ReaderWriterLock. Interlocked is the lightest weight thread sync feature available in .NET, and it should be used in favor of any other locking when possible. I would recommend dropping the RWL and just keep the Interlocked.
-
Hi everyone! I wanted to know how the ReaderWriterLock determines what is the "resource" in the code on which it has to accquire the reader lock or the writer lock, or how exactly does these locks work(internally). The MSDN article protects an static integer, but doesn't seem to specify how, as after acquiring a reader lock, writes can be done as in the following code:
ReaderWriterLock rw = new ReaderWriterLock();
int counter = 0;
try
{
rw.AcquireReaderLock(100);
try
{
Interlocked.Increment(ref counter);
}
finally
{
rw.ReleaseLock();
}
}
catch (ApplicationException)
{
Console.WriteLine("Failed");
}
Console.WriteLine(counter);here we are acquiring an reader lock for 100ms, but writing to the data. Confused :doh:
In addition to the other replies... Also keep in mind that the two methods of locking have different uses. A ReaderWriterLock is for synchronizing access to a resource that is read often but written to infrequently. That can give you better performance than locking on every read and write. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: