Improve ScreenInteractive wait function.

This commit is contained in:
ArthurSonzogni 2019-02-02 16:28:44 +01:00
parent 17cd6d25a5
commit 15da9cdc18
3 changed files with 11 additions and 15 deletions

View File

@ -7,7 +7,7 @@
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
using namespace ftxui; using namespace ftxui;
auto screen = ScreenInteractive::FixedSize(30, 3); auto screen = ScreenInteractive::TerminalOutput();
Menu menu; Menu menu;
menu.entries = {L"entry 1", L"entry 2", L"entry 3"}; menu.entries = {L"entry 1", L"entry 2", L"entry 3"};

View File

@ -40,7 +40,7 @@ class ScreenInteractive : public Screen {
Dimension dimension_ = Dimension::Fixed; Dimension dimension_ = Dimension::Fixed;
ScreenInteractive(int dimx, int dimy, Dimension dimension); ScreenInteractive(int dimx, int dimy, Dimension dimension);
std::condition_variable events_queue_wait; std::condition_variable events_queue_cv;
std::mutex events_queue_mutex; std::mutex events_queue_mutex;
std::queue<Event> events_queue; std::queue<Event> events_queue;
std::atomic<bool> quit_ = false; std::atomic<bool> quit_ = false;

View File

@ -74,22 +74,18 @@ ScreenInteractive ScreenInteractive::FitComponent() {
void ScreenInteractive::PostEvent(Event event) { void ScreenInteractive::PostEvent(Event event) {
std::unique_lock<std::mutex> lock(events_queue_mutex); std::unique_lock<std::mutex> lock(events_queue_mutex);
events_queue.push(event); events_queue.push(event);
events_queue_wait.notify_one(); events_queue_cv.notify_one();
} }
void ScreenInteractive::EventLoop(Component* component) { void ScreenInteractive::EventLoop(Component* component) {
bool handled = 0; std::unique_lock<std::mutex> lock(events_queue_mutex);
for (;;) { while (!quit_ && events_queue.empty())
std::unique_lock<std::mutex> lock(events_queue_mutex); events_queue_cv.wait(lock);
while (!events_queue.empty()) {
component->OnEvent(events_queue.front());
events_queue.pop();
handled = true;
}
if (handled) // After the wait, we own the lock.
return; while (!events_queue.empty()) {
events_queue_wait.wait(lock); component->OnEvent(events_queue.front());
events_queue.pop();
} }
} }