Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
New API Thread Call #5191
I'd like to suggest a new, portable thread call for yielding the processor to the next runnable thread. This is useful when writing lock-free message code and a producer/consumer detects a resource contention issue.
Justification vs Other Techniques
Since most libuv thread calls follow the 'pthreads' format, I'll call it:
The semantics for yielding a thread on POSIX vs Windows are fairly similar.
On POSIX (IEEE Std 1003.1-2008) this would generate a call to:
Which almost always succeeds. If it does fail, it returns -1 and sets errno. (On Linux it can never fail.)
(This is the POSIX generic equivalent to pthread_yield() and should be more portable).
On Windows API we would call:
Which has been available on the server-side since Win2K3 and on the desktop since WinXP. This call attempts to yield the CPU to the next runnable thread on the same processor.
On Windows it returns non-zero (BOOL true) if the processor yielded or zero if no runnable process on this processor exists. In this case no yield would have occurred.
On both platforms, if the yield fails you should probably return to a blocking wait condition (or an idle_sleep if nothing else). Of course this is app and situation dependent.
In libuv these semantics should probably be standardized to POSIX semantics so that
I'm not asking anyone else to write this code. It's fairly trivial, but I'd just like to suggest it so that I don't have to write #if defs in an app that uses a portable thread library.
(Question: Since uv_thread_yield can occur outside of an event_loop, how do you retrieve the uv_last_error which requires a loop?)