diff --git a/src/redox.cpp b/src/redox.cpp index 3436955..ae0431d 100644 --- a/src/redox.cpp +++ b/src/redox.cpp @@ -101,6 +101,10 @@ void Redox::run_event_loop() { while (!to_exit) { process_queued_commands(); ev_run(evloop, EVRUN_NOWAIT); + + // Wait until notified, or check every 100 milliseconds + unique_lock ul(loop_waiter_lock); + loop_waiter.wait_for(ul, chrono::milliseconds(100)); } cout << "[INFO] Stop signal detected." << endl; @@ -177,6 +181,9 @@ void Redox::command_callback(redisAsyncContext *ctx, void *r, void *privdata) { // Increment the Redox object command counter rdx->cmd_count++; + + // Notify to check the event loop + rdx->loop_waiter.notify_one(); } /** @@ -191,6 +198,10 @@ bool Redox::submit_to_server(Command* c) { c->invoke_error(REDOX_SEND_ERROR); return false; } + + // Notify to check the event loop + c->rdx->loop_waiter.notify_one(); + return true; } diff --git a/src/redox.hpp b/src/redox.hpp index 93de25e..01771c3 100644 --- a/src/redox.hpp +++ b/src/redox.hpp @@ -187,6 +187,10 @@ private: std::mutex exit_waiter_lock; std::condition_variable exit_waiter; + // Condition variable to check the event loop + std::condition_variable loop_waiter; + std::mutex loop_waiter_lock; + // Maps of each Command, fetchable by the unique ID number std::unordered_map*> commands_redis_reply; std::unordered_map*> commands_string_r;