Are there any libraries that bring ref
s, atom
s and agent
s to C code?
Are there also structural sharing libraries for C to accompany?
To my knowledge no.
Even if there was, IMHO it wouldn't be a particularly good fit for C code:
- These approaches depend heavily on the JVM to provide memory management and garbage collection. Structural sharing, in particular, implies that you can't easily determine who else is using a particular block of a data structure. So you really want automatic GC to clear this up when the last reference to a structural component disappears.
- The usefulness of the STM constructs is really in concurrent situations. It's much harder to write good concurrent code in C than in a JVM language where threading support is pervasive and more consistent across platforms / libraries.
- At least in the way that they are used in Clojure, the STM constructs are designed to be used in a functional programming language (i.e. a language where functions are pure, where you typically code by composing higher order functions and data is immutable). e.g. the function
swap!
for updating an atom is itself a higher order function.
While I'm not saying that you can't write functional-style STM code in C if you are determined enough.... it's not a good fit though, and you'd probably end up reinventing something like Lisp anyway. I'm reminded of Greenspun's tenth rule of programming:
Any sufficiently complicated C or Fortran program contains an ad hoc,
informally-specified, bug-ridden, slow implementation of half of
Common Lisp.
Basically, use the right tool for the job :-)