Exemple #1
0
void* factory_thread(void* arg)
{
	int i = (intptr_t)arg;
	/*printf("In factory thread %d\n", i);*/
	// mutex here 
	int time_wait;
	while(!stop_thread) {
		time_wait = rand()%4;
		printf("\tFactory %d ships candy & waits %ds\n", i, time_wait);
		//allocate new candy items
		candy_t* candy_ptr = malloc(sizeof(candy_t));
		candy_ptr->factory_number = i;
		candy_ptr->time_stamp_in_ms = current_time_in_ms();
		//insert it to the buffer
		bbuff_blocking_insert(candy_ptr);
		//process item into the stats module
		stats_record_produced(i);
		//sleep
		sleep(time_wait);
	}
	// When the thread finishes, print the message such as the following (for thread 0): “Candy-factory 0 done"
	printf("Candy-factory %d done\n", i);
	
	pthread_exit(NULL);
}
Exemple #2
0
void *factoryFunc(void *p){
	
	int fac = *((int *) p);
	srand(time(NULL));

	int countProduced=0;
	while(!stop_thread){
		int rand_sec = rand()%3;
		
		printf("\tFactory %d ships candy & waits %ds\n", fac, rand_sec );
		
		candy_t* candy = malloc(sizeof(candy_t));
		candy->factory_number = fac;
		candy->time_stamp_in_ms = current_time_in_ms();
		
		
		bbuff_blocking_insert(candy);
		countProduced++;
		stats_record_produced(fac);

		sleep(rand_sec);	
		free(candy);
			
	}
	//printf("candy created by %d = %d\n", fac, countProduced);
	printf("Candy-factory %d done\n", fac);
	
}