简体   繁体   中英

Multiple Tasks for one single Thread

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, eg

  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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM