Your code is not thread safe....
Protect your shared structures with mutexes and semaphores so only one process will access it at any given time.
Additional information
Your code thread safety is dependent on the compilers implementation of assignments like bThread2Done = true;
and test like if (bThread2Done) {...
which depending on
- the CPU architecture and
- the compiler optimization
may or may not happen atomically -- Assignments like a=b;
may not happen in one instruction or one cycle, so the other thread may read data which is not compactly written.
Compiler optimization may eliminate the actual test in if (bThread2Done) {...
if it is able to figure out that the variable is never set within the loop... there are hint you can give to the compiler that the variable is set outside the code (volatile) which may be needed.
Furthermore you code is implementing what is known as a spin-loop, which means that the threads are going to make the CPU 100% busy if they are locked out, which is really bad for your system performance in general.