Question

There is the following version of readers-writers problem: multiple readers and writers, 2 or more readers can read simultaneously, if a writer is writing no one can read or write, it is preferred if all writers get an equal chance to write (for example in 100 rounds 5 writers should write about 20 times each). What is the proper way to implement this in Clojure using STM? I'm not looking for a complete code, just some general directions.

Was it helpful?

Solution

Clojure's STM gives you much nicer guarantees than that. Writers wait for each other, but readers can still read while a writer is writing; it just sees the most-recent consistent state. If a writer isn't done writing yet, the reader doesn't see its changes at all.

OTHER TIPS

Clojure's built-in STM can't really include all the constraints you are looking for because readers never wait for writers and your requirements require readers to wait.

if you can forgive not blocking readers then you can go ahead and

(. (java.lang.Thread. #(dosync (write stuff)) start))
(. (java.lang.Thread. #(dosync (read stuff)) start))

if you need readers to block then you will need a different STM, the world has lots of them

As mentioned in other answers that readers don't block while reading and you want reader to block then you probably implement them as "writer" which write the same value it gets in its callback function. I know this is weird solution but may be this can help you out or give you some further directions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top