Subj : Re: Concurrent reads and sequential writes To : comp.programming.threads From : Torsten Robitzki Date : Tue Jul 26 2005 06:54 pm kozlovsky@kozlovsky-c4.sipo.gess.ethz.ch wrote: > Hello, > > I am writing a spider application on Linux, C++, STL, pthreads. > The application has a cache that contains (word, handle) pairs. > The Cache class has method "get", that, given a word, returns its > handle (if found in the cache), and method "add", that adds a > new pair to the cache. About 95% of cache access are get's. > > At present, the cache is synchronized with a single mutex, so > only a single thread can access the cache at any given time. > I would like to change synchronization in such a way that any > number of threads can call "get" at the same time, while "add" > must remain exclusive, i.e. only one thread can execute "add" > after blocking threads from starting new "get" and waiting > till threads currently executing "get" are all finished. > > I cannot figure out how to do it with mutexes, although this > seems to be a quite common situation, is it? Should I use > semaphores? Or may be sig_atomic_t? Why should I try to > reinvent the bycicle? Perhaps there is a known simple > solution to my problem. Please, help. I think You are looking for a read/write mutex. Have a look at pthread_rwlock_init() and friends. Lock the mutex for reading in the get() function and lock the mutex for writing in the add() function. regards Torsten .