10

Are there any libraries that bring refs, atoms and agents to C code?

Are there also structural sharing libraries for C to accompany?

Vi.
  • 37,014
  • 18
  • 93
  • 148
  • You can look at one of implementations code.google.com/p/rstm, example of usage code.google.com/p/rstm/wiki/WritingApps. Keep in mind that C is low level programming language so it's not correct to compare it with clojure. – mobyte Jan 21 '13 at 04:50
  • The following information is not worthy of a full answer, but I pulled this off Google Groups https://github.com/Chouser/clojure-jna – octopusgrabbus Jan 21 '13 at 18:32
  • There is more about this at this link https://groups.google.com/forum/?hl=en&fromgroups=#!topic/clojure/UWBpyHndluA – octopusgrabbus Jan 21 '13 at 19:57
  • octopusgrabbus, clojure-jna is not about that. It's more about integrating real Clojure with C code. I expecting some lightweight solution that does only STM and structural sharing (maybe with "manual" garbage collection limited to that structures), not the entire big JVM. – Vi. Jan 22 '13 at 12:29

1 Answers1

6

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 :-)

mikera
  • 105,238
  • 25
  • 256
  • 415