As mentioned in title, how can I execute the specific threads at specific time accurately?
Is there any library help to do it?
For example, [00:00:00.00, 00:05:00.00, 00:10:00.00..04:00:00.00, 04:05:00.00...]
CodePudding user response:
Use timer_create
with SIGEV_THREAD
and set repeating time in timer_set
to start a new thread at a repeated time interval.
CodePudding user response:
One simple way is to have a while(true)
loop which calls sleep(1);
and then in the loop checks what time it is with time(NULL)
and if the time for a thread is past due, start the corresponding thread.
CodePudding user response:
One simple way is using time()
to get the time:
#include <stdio.h>
#include <time.h>
void *get_sys_stat(void* arg)
{
// Do somthing hrear
}
int main()
{
hour = 5;
min = 30;
int status = 0;
time_t t = time(NULL);
pthread_t sys_stat_thread;
while(1) {
/* Get time*/
struct tm tm = *localtime(&t);
/* Trigger another process or thread */
if ((tm.tm_hour == hour) && (tm.tm_min == min))
pthread_create(&sys_stat_thread, NULL, get_sys_stat, NULL);
}
}