Lua Notes

All Files in examples/threads/
Login

Files in directory examples/threads in any check-in


C.threads examples

n4.nelua

Points of interest:

  1. it runs very slowly, taking 3-4 seconds and sleeping 30k times just to push out 10 messages
  2. 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).