For Programmers: Free Programming Magazines  


Home > Archive > C# > March 2004 > ReadWriterLock test example?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ReadWriterLock test example?
CJ

2004-03-28, 10:28 pm

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).
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com