Multiple Tasks for one single Thread

Viewed 42

In one of my systemd services I've implemented a thread to read in the information in /proc/stat and calculate the cpu usage for a specified interval. This thread runs every second and therefore i have the cpu load of each second.

Now I would like to add more tasks to this thread, so that it is not only running every 1 second, but also executing every 100 seconds and 1000 seconds.

I would like to store these values into seperate files, e.g.

  cpu_usage_last_second.txt
    // value is added each second

  cpu_usage_last_100_seconds.txt
    // value is added each 100 seconds

  cpu_usage_last_1000_seconds.txt
    // value is added each 1000 seconds

How can I achieve this with one thread, instead of creating a new thread for each task?

1 Answers

You can just count the number of seconds. When you get to a multiple of 100, do the 100 second thing. When you get to a multiple of 1000, do the 1000 second thing. If you want to prevent integer overflow after about 70 years, then reset the counter to 0 when it gets to 1000.

Related