What is the best practice behind handling timed events in C? The scenario I’m looking at is that i need to resend data to the server from the client if i do not receive a response from the server within a second.
Code would be nice, but explanation of the concept would be far more valuable.
Most operating systems have some form of timer. In Linux/Unix/Posix you have
alarm, and in windows there’sSetTimerSo, basiclaly, you send a message off, and set a timer to a time when you expect to have got a reply – 1s, 10s, 30s – whatever makes senses for your circumstances.
If you get a reply, you cancel the timer, and do whatever else you plan to do with the reply. If no reply arrives before the timer fires, you send again [it may mean “signal some semaphore or flag” to send again, rather than actually doing that in the handler for the timer].
For other operating systems, you’ll have to tell us what you are looking at, but most have some sort of mechanism to handle “tell me when X time has passed”.