Reader Authors Using STM in Clojure

There is the following version of the problem of readers and writers: several readers and writers, two or more readers can read at the same time, if the writer writes that no one can read or write, it is preferable that all writers receive equal chances to write (for example, in 100 rounds of 5 writers should write about 20 times each). What is the right way to implement this in Clojure using STM? I'm not looking for the full code, just some general guidelines.

+4
source share
3 answers

Clojure STM gives you a much better guarantee than this. Writers are waiting for each other, but readers can still read while the writer is writing; he just sees the most recent agreed state. If the writer is not written yet, the reader does not see the changes at all.

+3
source

Clojure's built-in STM cannot really include all the restrictions that you are looking for, because readers never wait for writers , and your requirements require readers to wait.

if you can forgive without 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, you will need another STM, there are many in the world.

+4
source

As mentioned in other answers, readers are not blocked while reading, and you want the reader to be blocked, you are probably implementing them as a β€œwriter” who write the same value that it receives in its callback function. I know this is a strange solution, but maybe it can help you or give you further guidance.

0
source

Source: https://habr.com/ru/post/1382546/


All Articles