[boost::thread] Starting thread at specific time
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!
On 5/03/2014 13:13, Quoth Kyle Ketterer:
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:
I would suggest using the Windows Task Scheduler to start the process at the appropriate time, rather than having it always running but idle until the designated moment. Saves resources and avoids bugs.
That's how I originally planned on doing it. The main issue I guess is that this is going to be pushed out via group policy. I have been told that it can be sometimes problematic. My other thought was to actually have two services - one strictly for the scheduler and one for the app itself. The scheduler service would then net stop the main service... Sent from my BlackBerry 10 smartphone. Original Message From: Gavin Lambert Sent: Tuesday, March 4, 2014 7:51 PM To: boost-users@lists.boost.org Reply To: boost-users@lists.boost.org Subject: Re: [Boost-users] [boost::thread] Starting thread at specific time On 5/03/2014 13:13, Quoth Kyle Ketterer:
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:
I would suggest using the Windows Task Scheduler to start the process at the appropriate time, rather than having it always running but idle until the designated moment. Saves resources and avoids bugs. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Le 05/03/14 01:13, Kyle Ketterer a écrit :
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: Hi,
in my todo list: Add a scheduled_executor that allows to run a task at a specific time or after a given duration. Unfortunately I'm too busy to implement it for now. I will come back to you when available. Best, Vicente
That would be excellent. I did a lot of experimenting with the cross platform sleep() and sleep_for() methods as well as the Windows API Sleep(). The conclusion I have from my tests, are that any type of sleep function ( on Windows at least) is absolutely unpredictable. For the precision I need to start a thread on a certain hour, I couldn't rely on sleep. I had to go the route of scheduled tasks for the app I'm developing. Linux has Sigalrm but Windows really has nothing to offer but the task scheduler. Sent from my BlackBerry 10 smartphone. Original Message From: Vicente J. Botet Escriba Sent: Sunday, March 9, 2014 9:59 AM To: boost-users@lists.boost.org Reply To: boost-users@lists.boost.org Subject: Re: [Boost-users] [boost::thread] Starting thread at specific time Le 05/03/14 01:13, Kyle Ketterer a écrit :
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: Hi,
in my todo list: Add a scheduled_executor that allows to run a task at a specific time or after a given duration. Unfortunately I'm too busy to implement it for now. I will come back to you when available. Best, Vicente _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Gavin Lambert
-
Kyle Ketterer
-
reminisc3@gmail.com
-
Vicente J. Botet Escriba