gpt4 book ai didi

c++ - 从不同的线程获取用户输入

转载 作者:行者123 更新时间:2023-11-28 05:33:16 25 4
gpt4 key购买 nike

我正在制作一个计时器程序,计算程序启动后耗时。在后台我也在检查键盘输入(输入/返回退出,点击窗口);这是在我作为分离运行的单独线程中完成的。

第二个线程似乎无法接收主线程的输入。当我使用键盘或鼠标时,没有任何反应。此外,屏幕上什么也没有出现,只有白色。

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);

轮询线程等待 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() 函数,那将非常有用。

关于c++ - 从不同的线程获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38925946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com