I have some code:
while(true) {
std::string message;
std::cin >> message; // break it
// send message
}
I want to interrupt waiting for user input from std::cin
from other thread.
How can I do it? Thanks in advance!
I have some code:
while(true) {
std::string message;
std::cin >> message; // break it
// send message
}
I want to interrupt waiting for user input from std::cin
from other thread.
How can I do it? Thanks in advance!
std::cin is a blocking call that can not be interrupted.
This thread here seems to have some non-blocking alternatives: https://stackoverflow.com/a/40812107/2562287
If you used a non blocking alternative for std::cin along with an boolean (threadsafe from either a mutex or being atomic, preferably the latter), you could set the boolean on the other thread and read the boolean on the input thread, if the boolean is either true or false then exit the input function.