I'm sort of a newbie to Clojure. I have multiple threads trying to write to an output stream and If I'm not mistaken sockets and their streams are not thread safe meaning bits can be mixed up if I write to them simultaneously. One of the main benefits of clojure is inbuilt concurrency handling of race conditions. How can I utilize this for my scenario?
I tried looking into atoms, refs and so on. I initially thought declaring the output stream as an atom would work but I'm not too sure, as it seems it avoids changing the atom state simultaneously (using swap!) however i think you can dereference an atom from multiple threads meaning multiple threads will deref the atom holding my output stream and write to it concurrently.
Any advise will be most helpful.
thanks in advance
(defn send-my-data [output data-bytes]
(try
(.write output)
(.flush output)
(catch Exception excp
(println (format "issue %s" (.printStackTrace excp))))
Now all my threads call this function anytime they want to write data to the output stream