Just to be sure - do Zig collections allow simultaneious Read-Only access?

Now I am using Mutex in order to allow simultaneious access to ArrayList/AutoArrayHashMap/…

I’d like to use RwLock for simultaneious Read-Only access from different threads

It looks like it wasn’t intended (nothing in comments and nothing in tests)

but

Hope dies last

Do Zig collections allow simultaneious Read-Only access?

ArrayList: Its read-only access only accesses a slice.

ArrayHashMap: Its read-only access requires calling the hash and eql in the context you pass in. If you pass an unthread-safe eql or hash, multi-threaded read-only access is not safe. Although I haven’t verified it, I believe that hash and eql in AutoContext(K) are thread-safe.

3 Likes

that’s exactly what RwLock is for, though if you never mutate the data (or at least not concurrently to the reads), and reading it doesn’t involve any mutations (like advancing the position of the next read) then there is no need for a lock. ArrayList can fit that description, so can the various hash maps, with the addition caveat of the context functions as @npc1054657282 mentioned.

2 Likes

In general not specific to Zig: If ALL access to something are read only (e.g. if calling a read function modifies a cache, it’s not read-only), everything independent of programming language (normally) is completely fine.

Problems only start to happen if you have (at least) one writing access.

4 Likes