Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

rwlock vs mutex performance

1,324 views
Skip to first unread message

Ales Pour

unread,
Dec 21, 1998, 3:00:00 AM12/21/98
to
Hello,
I'm trying to improve performance of a server I'm writing and since
I have several data structures in the program which are read many
times but modified rarely I've been considering to use rwlocks
instead of mutexes. I've found 2 implementations for POSIX threads
I'm programming with (LinuxThreads) - one is example from D. Buthenhof's
"Programming With POSIX(r) Threads" book and the other was posted
to this newsgroup by Sascha Schumann recently (the former uses mutex
and condition variables, the latter 2 mutexes). So I wrote a simple
test program where 10 reader threads and 1 writer thread competes for
shared vector (STL) of longs at maximum speed (writer thread tries to
get access to the vector less frequently so all threads end at about
same time). With both implementions of rwlocks the test program ran
about 2 times longer than with one-and-only-mutex solution!
I was expecting at least a bit of performance boost though...
Thanks very much for any comment,
Ales Pour

-------------------------------------------------------------------------

time ./test_it 10000000
-----------------------

mutex solution:

real 3m4.171s
user 0m59.480s
sys 2m4.690s

rwlock solution:

real 6m19.061s
user 1m59.860s
sys 4m19.200s

The writer thread does

{
for X times
write lock
modify values in vector
write unlock
}

and reader threads do

{
for Y times
read lock
find some number
read unlock
}

Douglas C. Schmidt

unread,
Dec 21, 1998, 3:00:00 AM12/21/98
to
Hi Ales,

> I'm trying to improve performance of a server I'm writing and since
> I have several data structures in the program which are read many
> times but modified rarely I've been considering to use rwlocks
> instead of mutexes. I've found 2 implementations for POSIX threads
> I'm programming with (LinuxThreads) - one is example from
> D. Buthenhof's "Programming With POSIX(r) Threads" book and the
> other was posted to this newsgroup by Sascha Schumann recently (the
> former uses mutex and condition variables, the latter 2 mutexes). So
> I wrote a simple test program where 10 reader threads and 1 writer
> thread competes for shared vector (STL) of longs at maximum speed
> (writer thread tries to get access to the vector less frequently so
> all threads end at about same time). With both implementions of
> rwlocks the test program ran about 2 times longer than with
> one-and-only-mutex solution! I was expecting at least a bit of
> performance boost though...

Unless you've got (1) a multi-processor machine with an efficient
thread implementation and/or (2) an application that's heavily I/O
bound you're almost always going to find that RW locks are slower than
mutexes since (1) they are more complicated and (2) without
multi-processors you don't actually get any real performance boost
since your threads will be serialized anyhow by the thread scheduler
on the single CPU.

BTW, this is why it's useful to build components that can have their
synchronization aspects STRATEGIZED. Please see

http://www.cs.wustl.edu/~schmidt/Concurrency.ps.gz

for more information on how to do this.

Take care,

Doug
--
Dr. Douglas C. Schmidt, Associate Professor
Department of Computer Science, Washington University
St. Louis, MO 63130. Work #: (314) 935-4215; FAX #: (314) 935-7302
sch...@cs.wustl.edu, www.cs.wustl.edu/~schmidt/

0 new messages