Is it safe to use a global hash table in an ocaml-5 parallel program?

The simplest possible example code of which use cases it was designed for will probably do a lot better than more verbiage.

I created two upstream issues to track improvement work on the Domain.DLS documentation:

I would like to submit PRs for this myself but haven’t found the time yet. Anyone should feel free to beat me to it and submit a documentation-improvement PR.

Sorry for reviving this thread but it leaves me rather perplexed. Are there any circumstances in which I would have to worry about thread(/domain?) safety with Map?

I’m starting to dabble with threads and my understanding was that maps being immutable would make it a no-brainer: say I have a process-wide Map.Make(String).t containing some immutable records and I have some threads make augmented versions for themselves with StringMap.add calls, I can’t imagine what could be unsafe since this leaves the original unaffected as far as I know. :thinking:

Sure but to be clear I’m asking whether Stdlib.Map specifically is safe to use given that it’s immutable. I had assumed it was, like records with immutable fields, integers, etc. but it’s been discussed here which is making me wonder what could possibly be a concern with it.

Ah yes I was wondering about that. The timing of your reply was suspicious. :wink:

The OCaml manual does not mention thread safety issues with Map (and they’re very good about that in other modules, like Hashtbl) but this discussion gave me doubts with its ideas of involving Atomic.t and what not. I guess they were debating how to update a shared map reference, which my project wouldn’t have.

Yes it’s 100% safe to use Map, Set, List and any other purely functional datastructure :slight_smile:

(You only need to worry if you imperatively update the shared memory between domains, but even then it’s not unsafe as in “my program will segfault”, it only means “my program can see out-of-date values and that can break my invariants and lead to wrong outcomes”)

If you have any doubts, I recommend that you run your program within a switch with 5.0.0+tsan as this version of the compiler will warn you of any unsafe behavior!

$ opam switch create 5.0.0+tsan
4 Likes