示例#1
0
void *kidFunc(void *p){
	
	srand(time(NULL));
	
	int countConsumed=0;
	while(1){
		
		candy_t* candy = bbuff_blocking_extract();
		if (candy){
			countConsumed++;
			stats_record_consumed(candy->factory_number, candy->time_stamp_in_ms);
		}
		
		sleep(rand()%1);
	}
	
	//printf("candy consumed from %d = %d\n", fac, count);
	return NULL;
}
示例#2
0
void* kid_thread(void* arg)
{	
	int i = (intptr_t)arg;
	/*printf("In kid thread %d\n", i);*/
	//mutex here
	int time_wait;
	for(;;) {
		time_wait = rand()%2;
		//extract from buffer
		candy_t* candy_ptr = (candy_t*) bbuff_blocking_extract();
		printf("\tKid %d eats candy & waits %ds\n", i, time_wait);
		//process item into the stats module
		if(candy_ptr!=NULL) {
			stats_record_consumed(candy_ptr->factory_number, current_time_in_ms()-candy_ptr->time_stamp_in_ms);
		}
		//free the extracted item
		free(candy_ptr);
		//sleep
		sleep(time_wait);
	}

	pthread_exit(NULL);
}