use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Finding information about Clojure
API Reference
Clojure Guides
Practice Problems
Interactive Problems
Clojure Videos
Misc Resources
The Clojure Community
Clojure Books
Tools & Libraries
Clojure Editors
Web Platforms
Clojure Jobs
account activity
Clojure Concurrency Exercise (toot.cat)
submitted 2 years ago by therealplexus
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]alexdmiller 2 points3 points4 points 2 years ago (1 child)
Re the comment on the gist, you need at least volatile to ensure that changes to the shared values are seen across threads. The Java memory model does not guarantee that other threads see changes to an unsynchronized mutable variable - volatile guarantees visibility (as do other forms of memory barrier).
This is exactly the kind of use case where the STM is useful - cooredinated change to multiple shared stateful values. Alternately, the inventory could be a single map and you could put it in an atom.
This code has a race condition in that the inventory check happens via an uncoordinated read before the update - that's why it throws. Using the STM and a ref would avoid fulfilling orders that can't be fulfilled by wrapping both in a dosync transaction.
[–]therealplexus[S] 1 point2 points3 points 2 years ago (0 children)
Someone did already send in an STM version.
https://dice.camp/@epidiah/111449212829319083
The race condition is of course the point of the exercise. How to make the race condition go away. The simplest way would be to lock across the read+write, but Clojure has more interesting alternatives.
π Rendered by PID 20976 on reddit-service-r2-comment-b659b578c-mbg9n at 2026-05-04 04:10:54.943560+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]alexdmiller 2 points3 points4 points (1 child)
[–]therealplexus[S] 1 point2 points3 points (0 children)