Files in directory examples/threads from the latest check-in
- n1.nelua
- n2.nelua
- n3.nelua
- n4.nelua
- n5.nelua
- README.md
C.threads examples
- n1.nelua - 'hello world' from another thread (
C.thrd_t) - n2.nelua - running two coroutines in their own threads
- n3.nelua - same, but main thread sleeps until detached workers are done (
C.thrd_detach) - n4.nelua - single producer/multiple consumers - with an interesting defect (
C.mtx_t,C.cnd_t) - n5.nelua - same, but without the defect
n4.nelua
Points of interest:
- it runs very slowly, taking 3-4 seconds and sleeping 30k times just to push out 10 messages
- this slowness isn't observable under
strace -vtff- in this case, the program runs quickly and as expected
The error: the main thread signals for the two workers to wake up and grab the mutex, but then the main thread wins the race to grab the mutex, sees that no worker has reset broadcast yet, then sleeps (while still holding the mutex), then wins the race again, repeat.
The error can be solved in two ways: by releasing the mutex before sleeping, as
n5.nelua does, or by never grabbing the mutex at all (which results in unsafe
interactions with the broadcast variable, without further changes).