|
Boost : |
Subject: Re: [boost] Boost MSM parallel behavior with delayed self-transitions?
From: degski (degski_at_[hidden])
Date: 2017-03-22 14:59:09
On 21 March 2017 at 20:31, Danylo Malyuta via Boost <boost_at_[hidden]>
wrote:
> Here is sample code which attempts to replicate the diagram, but just for
> the A state (the B state is not included, which would be trivial to do):
Just for fun I coded up the simple approach (no dependencies other than
STL, no threads). Note that plenty of code/headers is/are dedicated to
printing the current time, which for some reason or another is crazy
complex for such a simple and common task.
#include <chrono>
#include <thread>
#include <iostream> // Just to print the current time...
#include <iomanip> // Just to print the current time...
#include <ctime> // Just to print the current time...
namespace sc = std::chrono;
typedef sc::high_resolution_clock hrc;
void a_action ( ) {
std::time_t now_c = std::chrono::system_clock::to_time_t (
std::chrono::system_clock::now ( ) );
std::cout << "Trying A again at " << std::put_time ( std::localtime ( &
now_c ), "%T" ) << "...\n";
}
void b_action ( ) {
std::time_t now_c = std::chrono::system_clock::to_time_t (
std::chrono::system_clock::now ( ) );
std::cout << "Trying B again at " << std::put_time ( std::localtime ( &
now_c ), "%T" ) << "...\n";
}
int main ( ) {
sc::time_point < hrc > a_trigger = hrc::now ( ) + sc::seconds { 2 },
b_trigger = a_trigger + sc::seconds { 3 };
std::time_t now_c = std::chrono::system_clock::to_time_t (
std::chrono::system_clock::now ( ) );
std::cout << "Started at " << std::put_time ( std::localtime ( & now_c
), "%T" ) << '\n';
while ( true ) {
if ( a_trigger < b_trigger ) {
std::this_thread::sleep_until ( a_trigger );
a_action ( );
a_trigger += sc::seconds { 2 };
}
else {
std::this_thread::sleep_until ( b_trigger );
b_action ( );
b_trigger += sc::seconds { 5 };
}
}
return 0;
}
Sample output:
Started at 08:56:34
Trying A again at 08:56:36...
Trying A again at 08:56:38...
Trying B again at 08:56:39...
Trying A again at 08:56:40...
Trying A again at 08:56:42...
Trying B again at 08:56:44...
Trying A again at 08:56:44...
Trying A again at 08:56:46...
Trying A again at 08:56:48...
Trying B again at 08:56:49...
Trying A again at 08:56:50...
Trying A again at 08:56:52...
Trying B again at 08:56:54...
Trying A again at 08:56:54...
Trying A again at 08:56:56...
Trying A again at 08:56:58...
degski
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk