I have a real-time thread in Linux (3.4). Under certain conditions, I want it to relinquish control to other threads with the same priority, even if it hasn't finished using up its current timeslice. I was thinking of using the following code:
if (condition) {
resched_task();
cond_resched();
}
however, I don't see anyone else in the code doing this, making me think there's some other (better?) way of doing this. Is there a standard way to do this?
You can use the sched_yield() function to yield the rest of your time slice, as discussed here. sched_yield() causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static priority and a new thread gets to run.
I know sched_yield() is supposed to relinquish access to the thread so another thread can run. So it should move the thread it executes on to the back of the running queue. The code below is supposed to print 'abc' in order and then the newline after all three threads have executed.
The sched_yield() function allows a thread to give up control of a processor so that another thread can have the opportunity to run.
The sched_ yield( ) system call allows a process to relinquish the CPU voluntarily without being suspended; the process remains in a task_running state, but the scheduler puts it at the end of the runqueue list. In this way, other processes that have the same dynamic priority have a chance to run.
You can use the sched_yield() function to yield the rest of your time slice, as discussed here.
sched_yield() causes the calling thread to relinquish the CPU. The thread is moved to the end of the queue for its static priority and a new thread gets to run.
The question sounds like it's asking about kernel programming but the accepted answer is a function user mode API sched_yield(). I think the kernel answer to this question is schedule()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With