Home
Support

Knowledge Base

BSPs and drivers
Community resources
Product documentation
Questions?
Contact us

nanosleep() vs clock_nanosleep()
 
________________________________________________________________________

Applicable Environment
________________________________________________________________________
  • Topic: nanosleep() vs clock_nanosleep()
  • SDP: 6.5.0
  • Target: Any supported target
________________________________________________________________________

Recommendation
________________________________________________________________________

If you use nanosleep() to put a thread to sleep and in the meantime other processes may change CLOCK_REALTIME clock with ClockTime() or clock_settime(), then please consider using clock_nanosleep() and specify the clock_id to be CLOCK_MONOTONIC.

Here is the reason. There are two types of system clocks. The CLOCK_MONOTONIC clock keeps a monotonically increasing count of clock ticks since system bootup. The CLOCK_REALTIME keeps the time of day clock by adding an adjustment to the CLOCK_MONOTONIC clock count. When nanosleep() is called, a timeout value is calculated based on the the CLOCK_MONOTONIC count and the "TOD" adjustment at that moment. If a process calls ClockTime() to set a new time of day afterwards, the adjustment part will be changed according to the new value. When the right wakeup moment comes, the "TOD" adjustment is no longer the original value therefore nanosleep() misses the right wakeup moment.

________________________________________________________________________
NOTE: This entry has been validated against the SDP version listed above. Use caution when considering this advice for any other SDP version. For supported releases, please reach out to QNX Technical Support if you have any questions/concerns.
________________________________________________________________________


Related Attachments
 None Found





Please contact us with your questions or concerns.