Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a windows service, which is better, a wait-spin or a timer?

This question about Timers for windows services got me thinking:

Say I have (and I do) a windows service thats waiting on a WaitHandle and when woken, it dives into a wait-spin like I have shown below in a flowchart

wait spin diagram http://www.86th.org/waitspin.jpg

I'm curious if using a timer would be better than a wait-spin loop (sometimes called a spin-wait). I'll be honest, I've never used timers for anything other than my own tinkering.

I don't have any plans to switch unless the differences are profound and the benefits of using a Timer are amazing. However, I'm very interested on peoples thoughts on one vs the other for future development of this project.

Let me know if this should be a wiki

like image 672
Allen Rice Avatar asked Dec 30 '22 20:12

Allen Rice


1 Answers

I don't see you getting any benefit out of a timer. You are essentially behaving as a timer anyway with your sleep call, and you are not being a bandwidth hog since sleep yields up the time slice. Having an explicit timer wake up and call you is just going to complicate the code.

I wouldn't really say that you are doing a spin-wait, since I generally think of a spin-wait as something that doesn't sleep. It just burns all it's processor time waiting for the go signal.

like image 67
Christopher Avatar answered Jan 01 '23 10:01

Christopher