use condition_variable instead of loop to check bool

pull/126/head
moneroexamples 5 years ago
parent d3714734f1
commit 7f78ac3d0a

@ -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,

@ -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<std::mutex> lk(m);
s_shouldExit = true;
OMINFO << "Request to finish the openmonero recieved";
cv.notify_one();
}
bool shouldExit() const { return s_shouldExit; }
private:
static atomic<bool> s_shouldExit;
//Service& restbed_service;
static bool s_shouldExit;
};
atomic<bool> 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<std::mutex> lk(ExitHandler::m);
ExitHandler::cv.wait(lk, [&exitHandler]{
return exitHandler.shouldExit();});
}
//////////////////////////////////////////////
// Try to greacfully stop all threads/services

Loading…
Cancel
Save