From 7f78ac3d0a5dc3ad605d5eaf31a8e89e6e783a1b Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Sun, 20 Jan 2019 13:43:40 +0800 Subject: [PATCH] use condition_variable instead of loop to check bool --- config/config.json | 4 ++-- main.cpp | 36 ++++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/config/config.json b/config/config.json index bf57433..377637e 100755 --- a/config/config.json +++ b/config/config.json @@ -52,11 +52,11 @@ "viewkey" : "53c5850e895122574c53a4f952c726be3fe22bcd2b08f4bfed8946d887cc950b" } }, - "refresh_block_status_every_seconds" : 10, + "refresh_block_status_every_seconds" : 5, "blocks_search_lookahead" : 200, "search_thread_life_in_seconds" : 120, "max_number_of_blocks_to_import" : 132000, - "mysql_ping_every_seconds" : 10, + "mysql_ping_every_seconds" : 5, "ssl" : { "enable" : false, diff --git a/main.cpp b/main.cpp index 4cf179d..61a6c74 100755 --- a/main.cpp +++ b/main.cpp @@ -19,18 +19,26 @@ using boost::filesystem::path; class ExitHandler { public: + static std::mutex m; + static std::condition_variable cv; - static void exitHandler(int) { s_shouldExit = true; } + static void exitHandler(int) + { + std::lock_guard lk(m); + s_shouldExit = true; + OMINFO << "Request to finish the openmonero recieved"; + cv.notify_one(); + } bool shouldExit() const { return s_shouldExit; } private: - static atomic s_shouldExit; - //Service& restbed_service; + static bool s_shouldExit; }; -atomic ExitHandler::s_shouldExit {false}; - +bool ExitHandler::s_shouldExit {false}; +std::mutex ExitHandler::m; +std::condition_variable ExitHandler::cv; int main(int ac, const char* av[]) @@ -271,6 +279,10 @@ signal(SIGTERM, ExitHandler::exitHandler); signal(SIGINT, ExitHandler::exitHandler); + +// main restbed thread. this is where +// restbed will be running and handling +// requests std::thread restbed_service( [&service, &settings]() { @@ -279,14 +291,14 @@ std::thread restbed_service( }); - - // we are going to loop here for as long -// as control+c has been pressed -while (!exitHandler.shouldExit()) -{ - this_thread::sleep_for(100ms); -}; +// as control+c hasn't been pressed +{ + std::unique_lock lk(ExitHandler::m); + ExitHandler::cv.wait(lk, [&exitHandler]{ + return exitHandler.shouldExit();}); +} + ////////////////////////////////////////////// // Try to greacfully stop all threads/services