Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-blocking sleep timer in C

I'm looking for a good non-blocking sleep timer in C for windows.

Currently I am using sleep(10); which of course is a blocking timer.

Also I want it to consume no system resources, like my sleep timer it doesn't use any CPU or system resources which I am happy with.

So, what is the best non-blocking sleep timer I could use? And please also include an example of how to use it.

Thanks.

like image 811
luacoder Avatar asked Oct 14 '11 17:10

luacoder


1 Answers

You dont need an API you need to change your design.

A simple one is this.

You can have multiple threads, One is the Manager Thread and other are Worker Threads. At every 10 seconds the manager thread will wake up create a new worker thread and go to sleep. The worker threads will keep working even when the Manager is sleeping and this you have your non blocking effects.

I dont know how familar you are with threads, but here is a very very basic tutorial, that might get you started with this.

like image 89
anijhaw Avatar answered Nov 10 '22 08:11

anijhaw