Also included a small optimization change to avoid meaningless calls to WaitForSingleObject. As far as I can see it's completely thread-safe (one end only writes and the other end only reads), but I'm not completely sure if it will give any reasonable speed improvement. Anyway it's there.