PeriodicService.cpp
Go to the documentation of this file.
2 
3 using namespace swarmio;
4 using namespace swarmio::services;
5 
7 {
8  // Create clock
9  std::chrono::high_resolution_clock timer;
10  auto next = timer.now();
11 
12  // Go
13  for (;;)
14  {
15  // Check if we need to stop
17  {
18  break;
19  }
20 
21  // Perform update
22  Update();
23 
24  // Wait until the next iteration
25  next += _period;
26  std::this_thread::sleep_until(next);
27  }
28 }
29 
31 {
32  // Start worker thread
33  _worker = new std::thread(&PeriodicService::Worker, this);
34 }
35 
37 {
38  // Request shutdown
39  _shutdownRequested = true;
40 
41  // Wait for worker thread
42  _worker->join();
43 
44  // Delete worker thread
45  delete _worker;
46  _worker = nullptr;
47 }
std::atomic< bool > _shutdownRequested
Worker thread shutdown requested.
virtual void MailboxWillBeDisconnected() noexceptoverride
Called right before the mailbox is disconnected from its endpoint or if the attached endpoint is abou...
virtual void MailboxWasConnected() noexceptoverride
Called when the mailbox is attached to an already running endpoint or if the attached endpoint has ju...
void Worker()
Worker thread entry point.
std::thread * _worker
Worker thread.
std::chrono::milliseconds _period
Tick period.
virtual void Update()=0
Called periodically to perform work.


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48