Example #1
0
void * calc(void *args)
{
   /* 
    * compute total random points in the unit square
    * and return the number of hits in the sector (x*x + y*y < 1)
    *
    * uses global mypoints
    */

   double  x, y;                     /* random coordinates */
   long *  hits_p;                   /* pointer to number of hits */
   int     seed;                     /* seed for random generator */
   long    i;
   
   /* get memory for return value */
   hits_p = (long *) calloc(1, sizeof(long));        /* initialized  to 0 */

   /* initialize random generator (otherwise all return the same result!) */
   seed = (int) pthread_id();
   for(i=0; i<mypoints; i++) {
      x = ((double) rand_r(&seed))/RAND_MAX;
      y = ((double) rand_r(&seed))/RAND_MAX;
      pthread_yield();
      
      if ( x*x + y*y <= 1.0 ) {
         (*hits_p)++;
      }
   }

   return(hits_p); 
}
Example #2
0
void *yield_thread(void* arg)
{	
	/* Wait til all threads are created */
	pthread_barrier_wait(&barrier);
	for (int i = 0; i < nr_yield_loops; i++) {
		printf_safe("[A] pthread %d %p on vcore %d, itr: %d\n",
			    pthread_id(), pthread_self(), vcore_id(), i);
		/* Fakes some work by spinning a bit.  Amount varies per
		 * uth/vcore, scaled by fake_work */
		if (amt_fake_work)
			udelay(amt_fake_work * (pthread_id() * (vcore_id() +
								2)));
		pthread_yield();
		printf_safe("[A] pthread %p returned from yield on vcore %d, itr: %d\n",
		            pthread_self(), vcore_id(), i);
	}
	return (void*)(pthread_self());
}