NAME
mthr_yield - give up the processor in favour of another thread

SYNOPSIS
#include <mthreads>

void mthr_yield(void);

DESCRIPTION
mthr_yield() stops the execution of the calling thread and tries to schedule another thread. If there is no other thread, the call will return immediately, otherwise, the calling thread will be enqueued as the last thread on its processor's runqueue.

If there are threads on the runqueue, i.e. threads that had already been running, one of these threads will be taken. If the runqueue is empty, a newly created, but still not started thread will be taken. Be aware of this fact, as it could leed to unexpected behaviour! In the example below only two (!) threads will ever get started per processor and not all threads as might be expected:

#include <mthread.h>

void *yield_loop(void *arg)
{
  while(1)
  {
    printf("Thread %d will yield the processor\n",(int)arg);
    mthr_yield();
  }
}

void main()
{
  mthr_startup();

  for(i==;i<100;i++)
    mthr_create(0,DETACHED,CPU_LOCAL,yield_loop,(void *)i);
  mthr_exit(0);
}

SEE ALSO
mthreads(3), mthr_startup(3), mthr_create(3), mthr_exit(3)