next up previous contents
Next: The ISync class Up: Reference of Practical Thread Previous: Reference of Practical Thread

Switching Threads

# include  <ucr/ucr.h>
extern void thread_switch(THREAD*);
extern void thread_yield();

These two functions are still rather basic functions, but in fact can be implemented with the thread primitives, although the implementations in the uCR library use some internal details to improve code size and performance.

All the same, the following code demonstrates how one would implement these functions, and is similar to the actual uCR implementation:

# include <ucr/ucr.h>

void thread_yield()
{
      thread_runnable(thread_self());
      thread_reschedule();
}

void thread_switch(THREAD*tid)
{
      thread_runnable(tid, true);
      thread_runnable(thread_self());
      thread_reschedule();
}

The thread_yield() you've seen before. The thread_switch() function is slightly more interesting, in that it puts the new thread in the front of the run queue, and the current thread at the end.



Stephen Williams
9/2/1997