Shared memory examples with OCaml multicore and Hashtbl

I can’t find really examples in the domainslib repository of programs using shared-memory.
I can see examples using channels, tasks, but what if I just want to just use a global hashtbl and have different domains populating this hashtbl. Can I use the Mutex module for that? Does ocaml 4.12.0+domains understands Mutex? What if 2 domains use Hashtbl.add at the same time?

Ok I looked at Adapting the OCaml ecosystem for Multicore OCaml - Watch OCaml and apparently yes Mutex and so on work with domains.
The talk mention concurrent lock-free data structures but I could not find them though in domainslib. Like do we have a concurrent hashtbl available somewhere?

The talk above actually says that “Hashtbl” can be used from multiple domains at the same time.
How is that possible without data-race? I had a quick look at hashtbl.ml in the 4.12.0+domains variant and I don’t see any locking code in there. How 2 domains can call at the same time
Hashbl.add without any possible race?

It doesn’t mean that there won’t be data races – obviously there will be and you need a mutex. The talk is specifically discussing internal global state which is present in some modules such as Hashtbl. This global state would completely break multi-threaded code were it not for the fact that modifications were made. In other words, the modules were made reentrant but not thread-safe.

4 Likes

+1 to what @bluddy said. Lock-free data structures are here – although they are still work-in-progress. They will be made available in domainslib when things are a bit more stable.

2 Likes