I have a StringMap, which maps to struct instances, which reside on the heap. In each instance I have a mutex.
It is not possible to lock such a mutex, though. Locking exactly once triggers “unfair double lock”. Even when I step through the code with the debugger there is no second lock (there also is no second one in the source code).
Is this a bug in Zig or the standard-library, or am I missing something?
The problem was solved by having pointer to struct instead of struct as the data type of the hashmap. The double locks came from the hashmap implementation copying around data including the mutex.
Note that in the example, a lock is taken in the producer and the consumer. This provides synchronization between threads. You either need a lock or to use an atomic variable. A simple global variable doesn’t work reliably for this because there is no synchronization between threads.