I'm working on a Console
class for console IO (right now just input, though) and I'm polling input from std::cin
in my loop by having a background thread constantly checking for input. However, while it reads input fine, I (expectedly) ran into an issue where after I close my main window (GLFW) the console window is still sitting in the background waiting to either be closed out or receive input. I'm looking for a way to terminate the thread, but couldn't find any information on a good way to do it for this scenario. Any ideas?
console.h
class Console
{
public:
Console();
~Console();
bool isInputAvailable();
std::string pullLastInput();
private:
bool do_quit;
void thread_input();
std::thread in_thread;
std::queue<std::string> input_queue;
};
console.cpp:
Console::Console() : in_thread(&Console::thread_input, this)
{
do_quit = false;
}
Console::~Console()
{
do_quit = true;
in_thread.join();
}
bool Console::isInputAvailable()
{
return input_queue.size() > 0;
}
std::string Console::pullLastInput()
{
std::string input;
input = input_queue.front();
input_queue.pop();
return input;
}
void Console::thread_input()
{
std::string input;
while (!do_quit)
{
std::cin >> input;
input_queue.push(input);
}
}
Aucun commentaire:
Enregistrer un commentaire