I need to do some operation at regular intervals in my thread. What is the best method to do this? Like if, i have a local socket communication between linux deamon and a android application. In this case, if i want to send data periodically to the android app from deamon, how can i proceed?
-
2Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour) and read [How to Ask](http://stackoverflow.com/help/how-to-ask) to learn what we expect from questions. See [On-topic Reference](http://stackoverflow.com/help/on-topic) – Sourav Ghosh May 13 '15 at 12:32
-
the way i used to implement this is via "select" . we can lock the while loop using the select call. we can give the timeout value in the select call and then get the timer value.... – JVN May 19 '15 at 12:50
-
please help me to add answer for this question . i have found the answer for this. but the question got closed. – JVN Jun 23 '15 at 10:37
2 Answers
If you use Linux why not try sleep()
or usleep()
functions of unistd.h
?
sleep(5);
Will pause the thread for five seconds and then resume execution.
sleep(sec)
The
sleep()
function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process.
usleep(usec)
usleep()
function suspends execution of the calling thread for (at least) usec microseconds.

- 43,482
- 10
- 63
- 98
You should use Simple Signals - C programming and alarm function if you want truely accurate timing. If you don't do this, using sleep()
etc will eventually result in your timer shifting due to the overhead of your code, the OS, etc.