Is this a bug in multithreading code?