接收来自不同线程的用户输入
Taking in user input from a different thread
我正在制作一个计时器程序,计算自程序启动以来经过的时间。在后台,我也检查键盘输入(进入/返回退出,点击窗口);这是在一个单独的线程中完成的,我已经运行为detached。
似乎第二个线程无法接收来自主线程的输入。当我使用键盘或鼠标时,什么也没发生。而且,屏幕上什么也没有,只有白色。
std::mutex g_mutex;
std::condition_variable cv;
// check for input from the user using the window object
// sets stopProgram to true if the user wishes to exit
void poll(sf::RenderWindow& window, bool& stopProgram) {
std::unique_lock<std::mutex> lk(g_mutex);
// wait for main thread to open window
cv.wait(lk, [&] { return !stopProgram && window.isOpen(); });
sf::Event event;
while (true) {
if (window.pollEvent(event)) {
// if user wants to exit program
if (event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed &&
(event.key.code == sf::Keyboard::Return || event.key.code == sf::Keyboard::Escape))) {
window.close();
// main thread will explicitly exit the main loop
stopProgram = true;
break;
}
}
}
}
int main()
{
int hour = 0, minute = 0, second = 0;
auto text = textObject();
bool stopProgram = false;
// run a background thread that checks for input while the main program runs
std::thread(poll, std::ref(window), std::ref(stopProgram)).detach();
std::once_flag flag;
std::lock_guard<std::mutex> lk(g_mutex);
while (window.isOpen()) {
// notify once window opens
std::call_once(flag, [&] { cv.notify_one(); });
// set timestamp
text->setString(makeTimeStamp(hour, minute, second));
// if the background thread set stopProgram, end the program
if (stopProgram) break;
window.clear(sf::Color::White);
window.draw(*text);
window.display();
// update time
second = (second + 1) % MAX_SEC;
if (second == 0) minute = (minute + 1) % MAX_MIN;
if (second == 0 && minute == 0) hour = (hour + 1) % MAX_HOUR;
// sleep one second
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
我使用多线程是正确的吗?如果是这样,只能主线程接收输入,这就是为什么它不工作?
更新:如果我摆脱了while (true)
并使用while (window.pollEvent(event))
并将lock_guard
移动到if (stopProgram)
之前,然后文本(时间戳)出现在屏幕上,但我仍然无法处理输入。
主线程启动轮询线程。
std::thread(poll, std::ref(window), std::ref(stopProgram)).detach();
主线程获取g_mutex并且永远不会释放它。
std::lock_guard<std::mutex> lk(g_mutex);
poll线程等待g_mutex被释放:
std::unique_lock<std::mutex> lk(g_mutex);
但是主线程永远不会释放它,所以轮询线程永远不会做任何事情。
修复它。修改main()函数的开头:
int main()
{
int hour = 0, minute = 0, second = 0;
auto text = textObject();
volatile bool stopProgram = false;
// run a background thread that checks for input while the main program runs
std::thread(poll, std::ref(window), std::ref(stopProgram)).detach();
while (!window.isOpen()) { /* busy loop */ }
{
std::lock_guard<std::mutex> lk(g_mutex);
cv.notify_all();
}
while (window.isOpen()) {
...
这个SFML API使事情比我使用过的其他窗口框架更加困难。如果有一个线程安全的window.pushCustomEvent()函数,那就太有用了。
相关文章:
- 使用 task_group 的英特尔线程构建模块性能不佳(新用户)
- 用于在C++中计算用户数据的线程
- 如何将用户输入从控制台传递到不同线程中的对象 -> QTcpsocket
- 如何将每线程用户数据传递到ASIO处理程序中
- 如何在不让用户等待的情况下管理多个线程
- C++等待用户输入的中断线程
- 用户i/o线程C
- 在用户模式下从另一个进程回调,没有额外的线程
- 根据用户请求停止tbb线程
- 程序基于用户输入创建多个线程不起作用(cin)
- 在多线程程序中通过用户事件传递短数据的正确方法是什么
- std::线程在用户或内核空间中进行管理
- 用户模式计划程序线程的最大数量
- xiAPI:无法更改线程调度程序,请检查实时优先级的用户限制
- Asio内部线程对用户透明吗?
- 选择线程:超时和用户输入
- 多线程使用用户定义的对象
- 如何调度/创建用户级线程,以及如何创建内核级线程
- 用户空间线程库
- 线程安全的用户数据与这个websocket++示例