#include #include #include #include "condition_variable_steady.hpp" #include #include #include #include #define WAIT_UNTIL 1 using namespace std; using namespace boost; namespace ch = boost::chrono; namespace po = boost::program_options; int main(int argc, char ** argv) { string type; uint32_t sleepTime; po::options_description desc("Arguments"); desc.add_options() ("help", "Help message") ("type", po::value(&type)->default_value("steady"), "Type of test: steady, until (wait_until), for (wait_for) or sleep (sleep_until)") ("time", po::value(&sleepTime)->default_value(10), "Sleep time (sec)"); try { po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); if (vm.count("help")) throw po::error(""); } catch(const std::exception&) { cout << desc << endl; return 1; } ch::steady_clock::time_point startSteady, endSteady; ch::system_clock::time_point startSystem, endSystem; startSteady = ch::steady_clock::now(); startSystem = ch::system_clock::now(); boost::mutex signalLock; boost::condition_variable signal; // Signal for Timer Queue Processing thread boost::condition_variable_steady steady_signal; cout << "Waiting for " << sleepTime << " sec" << endl; if (type == "sleep") { cout << "SLEEP_UNTIL" << endl; boost::this_thread::sleep_until(startSteady + ch::seconds(sleepTime)); } else if (type == "steady") { cout << "STEADY_WAIT" << endl; boost::unique_lock lock(signalLock); ch::time_point wait_time = startSteady + ch::seconds(sleepTime); steady_signal.wait_until(lock, wait_time); } else if (type == "until") { cout << "WAIT_UNTIL" << endl; boost::unique_lock lock(signalLock); ch::time_point wait_time = startSteady + ch::seconds(sleepTime); signal.wait_until(lock, wait_time); } else if (type == "for") { cout << "WAIT_FOR" << endl; boost::unique_lock lock(signalLock); signal.wait_for(lock, ch::seconds(sleepTime)); } else { cout << "Unexpected test type: '" << type << "'" << endl; return 1; } endSteady = ch::steady_clock::now(); endSystem = ch::system_clock::now(); int ms = ch::duration_cast(endSteady - startSteady).count(); cout << "Steady duration/start/stop: " << (float)ms / 1000.0 << endl; cout << " " << startSteady << " / " << endSteady << endl; cout << "System duration/start/stop: " << ch::duration_cast(endSystem - startSystem) << endl; cout << " " << startSystem << " / " << endSystem << endl; }