user1264182
user1264182

Reputation: 21

usleep busy waiting

started to experience with some real time threads. I just sent my thread to sleep by calling usleep(TIME). This will guarantee the thread to sleep for at least TIME. I now measured with TIME= 10ms and got a gaussian function with an expected value of µ=10,1ms The second measuring with TIME = 1us: I get a big peak at 4us.

So why is the second measurement not gaussian? My first idea was, the thread does not really sleep, but it makes a busy waiting instead. As stated here (old beahavior) there is no busy waiting. How can one explain the two curves?

Upvotes: 2

Views: 1707

Answers (2)

perreal
perreal

Reputation: 98118

This can be explained with the overhead of the function call and perhaps a different method of waiting for a smaller interval. You can consider a constant the delay that cannot be avoided but can be subtracted for larger waits.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 882716

Because of the resolution of timing functions. You said it yourself, it guarantees to sleep for at least that time. The reason why it can't guarantee to sleep exactly that amount of time is because it most likely operates in four-microsecond intervals.

The man page also mentions this phenomenon:

The sleep may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers.

Upvotes: 4

Related Questions