开发者

Sleep a thread for an indefinite amount of time in Linux

开发者 https://www.devze.com 2023-04-01 23:25 出处:网络
I want to make a thread sleep for an indefinite amount of time. The reason I want to do this is because my program only takes action when it receives a signal and has nothing开发者_StackOverflow to do

I want to make a thread sleep for an indefinite amount of time. The reason I want to do this is because my program only takes action when it receives a signal and has nothing开发者_StackOverflow to do in the primary thread. Therefore, all processing is done inside the signal handler. How can I sleep for an indefinite amount of time?


I believe you're looking for the pause function:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html

You could do something like: for (;;) pause();


If you're just doing something on another thread, simply call pthread_join on that thread and it will pretty much block "forever". You could achieve the same effect using a condition variable.


Use semaphores!

Have your thread blocked on a semaphore by using sem_wait. Once you need to wake your thread signal the semaphore by using sem_post from another thread.


POSIX provides the sigsuspend function to wait for a signal. (As mentioned in another answer, pause works as well.)

0

精彩评论

暂无评论...
验证码 换一张
取 消