|
| I'm writing a multi-threaded app, where lots of threads will be
reading a variable and occasionally a thread may write to it.
I assume that this variable needs to be protected against concurrent
access by a writer and a reader (concurrent access by multiple readers
is OK). I can use a lock statement to protect the variable, but it
seems as though a ReadWriterLock would be more efficient. I would,
however, like to test that it is working correctly - except that I
can't devise an example to test this! Could anyone give me some
simple code where I could run it with and without the ReadWriterLock
and see how the results differ?
For example, I used the following method to test the lock statement:
public int Func2(int val)
{
lock(typeof(CM))
{
intCnt = val;
Thread.Sleep(1000);
return intCnt;
}
}
and called this from multiple threads, checking whether the value I
sent is the same as the value I receive back (where if the lock
statement is commented out, it generally isn't the same, whereas if I
leave the lock statement in, it always is the same).
|
|