#include <pthread.h>
#include <stdlib.h>
#include <time.h>

static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;

int yield_thread(double prob) {
   double ran;
   struct timespec rqt;

   pthread_mutex_lock(&mylock);
   ran = drand48();
   pthread_mutex_unlock(&mylock);
   if (ran > prob)
      return 0;                                             /* do not yield */
   rqt.tv_nsec = 1000;        /* try to sleep for 1 microsecond, maybe more */
   rqt.tv_sec = 0;
   nanosleep(&rqt,NULL);
   return 1;
}

