6

This is an interview question. How do you implement a read/write mutex? There will be multiple threads reading and writing to a resource. I'm not sure how to go about it. If there's any information needed, please let me know.

Update: I'm not sure if my statement above is valid/understandable. But what I really want to know is how do you implement multiple read and multiple writes on a single object in terms of mutex and other synchronization objects needed?

jasonline
  • 8,646
  • 19
  • 59
  • 80
  • Are you talking about a normal mutex or a Multiple Read/Single Write mutex implemented with mutexes/semaphores/condition variables? – stefaanv Feb 25 '10 at 14:23
  • @stefaanv: I'm not sure but I think it is for multiple read/multiple write mutex implemented with mutexes/semaphres/condition variables. Is there such thing/way? – jasonline Feb 25 '10 at 14:37
  • Multiple Read/Single Write mutexes are not straightforward, I just had a shot at it and failed :-( so here is the wikipedia link: http://en.wikipedia.org/wiki/Readers-writer_lock – stefaanv Feb 25 '10 at 14:59
  • How do you implement a certain mutex using what? You need some sort of basic synchronization capability, and I doubt the fundamentals are the same on all platforms. This badly needs a platform tag at the very least. – David Thornley Feb 25 '10 at 15:35
  • this answer shows a MRSW mutex using boost: http://stackoverflow.com/questions/989795/example-for-boost-shared-mutex-multiple-reads-one-write/989816#989816 – stefaanv Feb 25 '10 at 15:45
  • @jason: I just re-read your comment and there is no such thing as multiple read/multiple write mutex. That would mean you don't have to use a mutex. A read write mutex only needing to allow 1 reader or writer is a normal mutex and can be implemented as a binary semaphore (start value = 1), so that lead me to believe that you were talking about MRSW, eventhough I seem to be the only one. – stefaanv Feb 26 '10 at 07:43
  • @stefaanv: Sorry if my terms may seem incorrect. How do you implement multiple read and multiple writes on a single object in terms of mutex and other synchronization objects needed? I've updated my question. – jasonline Feb 26 '10 at 07:59

4 Answers4

13

Check out Dekker's algorithm.

Dekker's algorithm is the first known correct solution to the mutual exclusion problem in concurrent programming. The solution is attributed to Dutch mathematician Th. J. Dekker by Edsger W. Dijkstra in his manuscript on cooperating sequential processes. It allows two threads to share a single-use resource without conflict, using only shared memory for communication.

Note that Dekker's algorithm uses a spinlock (not a busy waiting) technique.
(Th. J. Dekker's solution, mentioned by E. W. Dijkstra in his EWD1303 paper) alt text

Community
  • 1
  • 1
Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
1

The short answer is that it is surprisingly difficult to roll your own read/write lock. It's very easy to miss a very subtle timing problem that could result in deadlock, two threads both thinking they have an "exclusive" lock, etc.

In a nutshell, you need to keep a count of how many readers are active at any particular time. Only when the number of active readers is zero, should you grant a thread write access. There are some design choices as to whether readers or writers are given priority. (Often, you want to give writers the priority, on the assumption that writing is done less frequently.) The (surprisingly) tricky part is to ensure that no writer is given access when there are readers, or vice versa.

There is an excellent MSDN article, "Compound Win32 Synchronization Objects" that takes you through the creation of a reader/writer lock. It starts simple, then grows more complicated to handle all the corner cases. One thing that stood out was that they showed a sample that looked perfectly good-- then they would explain why it wouldn't actually work. Had they not pointed out the problems, you might have never noticed. Well worth a read.

Hope this is helpful.

Eric Pi
  • 1,904
  • 12
  • 13
0

This sounds like an rather difficult question for an interview; I would not "implement" a read/write mutex, in the sense of writing one from scratch--there are much better off-the-shelf solutions available. The sensible real world thing would be to use an existing mutex type. Perhaps what they really wanted to know was how you would use such a type?

Polyfun
  • 9,479
  • 4
  • 31
  • 39
  • ShellShock: Can a read/write mutex not be implemented in terms of a regular mutex and a semaphore perhaps? I'm not familiar with semaphores but I got a feeling the interviewer is leading me to that solution. – jasonline Feb 25 '10 at 14:10
  • 1
    Any example for off-the-shelf solutions you can recommend? – jasonline Feb 25 '10 at 14:14
0

Afaik you need either an atomic compare-and-swap instruction, or you need to be able to disable interrupts. See Compare-and-swap on wikipedia. At least, that's how an OS would implement it. If you have an operating system, stand on it's shoulders, and use an existing library (boost for example).

Jan
  • 1,807
  • 13
  • 26