Adding more rigor to shutdown process for processor service.
This commit is contained in:
parent
97d11b4016
commit
8d6fe8fa3c
3 changed files with 18 additions and 5 deletions
|
@ -779,8 +779,11 @@ size_t rai::processor_service::poll ()
|
|||
void rai::processor_service::add (std::chrono::system_clock::time_point const & wakeup_a, std::function <void ()> const & operation)
|
||||
{
|
||||
std::lock_guard <std::mutex> lock (mutex);
|
||||
operations.push (rai::operation ({wakeup_a, operation}));
|
||||
condition.notify_all ();
|
||||
if (!done)
|
||||
{
|
||||
operations.push (rai::operation ({wakeup_a, operation}));
|
||||
condition.notify_all ();
|
||||
}
|
||||
}
|
||||
|
||||
rai::processor_service::processor_service () :
|
||||
|
@ -792,6 +795,10 @@ void rai::processor_service::stop ()
|
|||
{
|
||||
std::lock_guard <std::mutex> lock (mutex);
|
||||
done = true;
|
||||
while (!operations.empty ())
|
||||
{
|
||||
operations.pop ();
|
||||
}
|
||||
condition.notify_all ();
|
||||
}
|
||||
|
||||
|
|
|
@ -305,7 +305,6 @@ namespace rai {
|
|||
void stop ();
|
||||
bool stopped ();
|
||||
size_t size ();
|
||||
private:
|
||||
bool done;
|
||||
std::mutex mutex;
|
||||
std::condition_variable condition;
|
||||
|
|
|
@ -128,7 +128,14 @@ TEST (processor_service, top_execution)
|
|||
ASSERT_EQ (1, value);
|
||||
}
|
||||
|
||||
TEST (processor_service, add_stopped)
|
||||
TEST (processor_service, stopping)
|
||||
{
|
||||
|
||||
rai::processor_service service;
|
||||
ASSERT_EQ (0, service.operations.size ());
|
||||
service.add (std::chrono::system_clock::now (), [] () {});
|
||||
ASSERT_EQ (1, service.operations.size ());
|
||||
service.stop ();
|
||||
ASSERT_EQ (0, service.operations.size ());
|
||||
service.add (std::chrono::system_clock::now (), [] () {});
|
||||
ASSERT_EQ (0, service.operations.size ());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue