| Main index | Section 9 | Options | 
| PRI_USER | Schedule the thread with its base user priority; the value corresponding to setpriority(2) / nice(3). | 
| PRI_UNCHANGED | |
| Yield the thread without changing its priority. | |
The should_yield() function checks if sufficient time has passed since the thread's last voluntary context switch that yielding would be a useful service to other threads. It returns true when this is the case. See USAGE NOTES for an elaboration of what this means.
The maybe_yield() function is a helper function for the common task of optionally yielding the processor. Internally, kern_yield(PRI_USER) will be called if should_yield() returns true.
The scheduler aims to identify threads which monopolize the CPU, and will schedule them with decreased priority. Threads which regularly yield the processor will be given the chance to run more often. The possibly surprising effect of this is that, depending on the disposition of other threads on the CPU's runqueue, a call to kern_yield() does not guarantee that the yielding thread will be taken off the CPU.
With the above considerations in mind, it is advised that code written using kern_yield() be measured to confirm that its use has a positive effect on relevant performance or responsiveness metrics. Switching thread contexts has a non-zero cost, and thus yielding the processor too eagerly could have a negative impact on performance.
Additionally, since yielding is a cooperative action, it is advised that the yielding thread release any locks that it may be holding, when possible. Otherwise, threads which have been given the chance to run could end up waiting on the yielding thread to release the lock, largely defeating the purpose of the yield.
| KERN_YIELD (9) | January 30, 2023 | 
| Main index | Section 9 | Options | 
Please direct any comments about this manual page service to Ben Bullock. Privacy policy.
| “ | When people say "Drive safe!" I'm like no, a safe is for keeping money, I drive car. | ” | 
| — Artur Bagyants | ||