sleep system call in thread
I am using pthread library for multi-threadi开发者_运维知识库ng. Inside thread function, I use sleep system call. Will this block a single thread or the whole process. Thanks.
Generally, sleep
affects only the calling thread. Real, kernel-managed threads run independently of each other. In an app that has "green" threads, though (not native to the OS; managed by the app itself), a system call that blocks may block everything. But that kind of brokenness is rather rare -- software managing green threads tends to provide a whole runtime environment, including ways to sleep
without resorting to a system call.
The better question is...do you really need to sleep
at all? Time-based synchronization tends to lead to race conditions and fragile apps. There's a way for threads to wait on and trigger each other; that leads to better determinism.
精彩评论