|
Boost Users : |
Subject: [Boost-users] [boost::thread] Starting thread at specific time
From: Kyle Ketterer (reminisc3_at_[hidden])
Date: 2014-03-04 19:13:09
Hello all,
I am working on an application which I originally wrote as a console app
and am now transforming into a Windows service. Basically, looking for a
little advice.
My goal is to have an independent service which starts at a specific time
and only runs for a set amount of hours. The code I am currently
contemplating looks like this:
----------------------------------------------------------------
int start_hour = 18; //6pm
time_t t = time(0); //current time
struct tm* now = localtime ( &t );
//total time the app should run for
boost::posix_time::time_duration run_duration ( 10, 0, 0, 0 );
//current time relative duration
boost::posix_time::time_duration start_duration ( now->tm_hour,
now->tm_min, now->tm_sec );
//get duration until service start
boost::posix_time::time_duration diff =
boost::posix_time::hours(start_hour) - boost::posix_time::milliseconds
(start_duration.total_milliseconds() );
//dont start thread until designated start time
boost::thread thr ( &ServiceWorkerThread );
//sleep this thread until ready to start
boost::this_thread::sleep( boost::posix_time::milliseconds(
diff.total_milliseconds() ) );
if ( thr.timed_join( run_duration ) )
{
//do stuff
}
-------------------------------------------------------------
Basically, I am trying to make use of the time_duration class to achieve
this. Based on the code above, I want to run the service for 10 hours, but
not start it until the correct hour.
Somewhere near the end of the service entry I will restart the service and
the service *should* until the correct time and only execute for X hours.
I am looking for further suggestions/improvements based on the above.
Thanks!
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net