I've been experiencing a strange occasionally occurring bug for the last few days.
I have a console application that also displays a window opened with SDL for graphical output continuously running three threads. The main thread runs the event loop, and processes the console input. The second thread uses
inputAvailable_ = true; // this is atomic
As @davmac suggested,
peek appears to have been interfering with
getline. My assumption would be that this is linked to
peek taking a character and then putting it back at the same time as
getline takes the buffer.
Whatever the underlying cause of the issue is, I am >98% sure that the problem has been fixed by implementing the fix davmac suggested. In several hours of use I have had no issues.
Moral, don't concurrently access
cin, even if one of the functions doesn't modify the stream.
(Note, the above happened on both g++ and clang++ so I assume that it's just linked to the way the
std library is frequently implemented).
As @DavidSchwartz pointed out, concurrent access to streams is explicitly prohibited, so that clearly explains why the fix works.