As per This Question, I'm using a thread to terminate a function on user input. My code looks something like:
bool stopper = false;
thread stopThread(userStop, &stopper); // start thread looking for user input
for(int i = 0; i < 1000; i++) {
if(stopper) { break; } // break if desired
// Do stuff
}
return 0;
where,
userStop(bool *st) {
char chChar = getchar();
if(chChar == '
') {
*st = true;
}
}
When I run this, I get the error terminate called without an active exception
. Based on these questions: thread terminate called without an active exception, C++ terminate called without an active exception; it looks like its because I'm not 'join'ing the thread again.
The problem is, I don't want to 'join' the thread -- because then the user will need to provide input for userStop()
to terminate, but I only want the user to provide input if the for-loop is to be broken (which it isn't necessarily).
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…