Ejemplo n.º 1
0
int
main(int argc, char **argv)
{
	int n, e, item;
	unsigned seed, prod_sum, cons_sum;

 	if (argc > 1 && !strcmp("-", argv[1])) {
 		printf("usage: %s num_items busy_loops\n", argv[0]);
		exit(1);
	}

	if (argc > 1) {
		num_items = atoi(argv[1]);
		if (num_items < 0)
			num_items = 0;
	}
	if (argc > 2)
		busy_loops = atoi(argv[2]);
	printf("num_items %d busy-loops %d\n", num_items, busy_loops);

	seed = prod_sum = cons_sum = 0;
	for (n = 0; n < num_items; ++n) {
		item = ProduceItem(&seed, &prod_sum);
		ConsumeItem(item, &cons_sum);
	}

	printf("prot_sum %u cons_sum %u\n", prod_sum, cons_sum);
	if (cons_sum != prod_sum)
		printf("ERROR\n");

	return 0;
}
Ejemplo n.º 2
0
void *
Consumer(void *v)
{
	struct thread_data *data = (struct thread_data *)v;
	unsigned sum = 0;
	int n, item;
	int waits = 0;
	int l = maxbuflen - 1;
	
	for (n = 0; ; ++n) {
		Pthread_mutex_lock(&buf_mutex);
		while (buflen == 0) {
			if (eof) {
				Pthread_mutex_unlock(&buf_mutex);
				goto done;
			}
			++waits;
			Pthread_cond_wait(&empty_cond, &buf_mutex);
		}
		item = buf[--buflen];
		if (buflen == l)
			Pthread_cond_broadcast(&full_cond);
		Pthread_mutex_unlock(&buf_mutex);

		ConsumeItem(item, &sum);
	}

done:
	data->sum = sum;
	printf("Consumer %d exit, %d items received, %d waits\n", 
		data->no, n, waits);
	return data;
}
void ConsumerTask()
{
	static int cnt = 0;
	while(1)
	{
		sleep(1);
		int item = ConsumeItem(&gItemRepository);
		std::cout << "Consume the"<<item << "^th item"<< std::endl;
		if(++cnt == kItemsToProduce)
		{
			break;
		}
	}
}
Ejemplo n.º 4
0
void *
Consumer(void *v)
{
	struct consumer *c = (struct consumer *)v;
	struct producer *p;
	struct packet *pa, *old;
	unsigned sum = 0;
	int n, k;
	int waits = 0;
	int item_cnt = 0;
	
	for (n = 0; ; ++n) {
		/* Hole Packet aus lokaler fullPacketQueue */
		Pthread_mutex_lock(&c->queue_mutex);
		while (!c->fullPacketQueue) {
			if (c->eof) {
				Pthread_mutex_unlock(&c->queue_mutex);
				goto done;
			}
			++waits;
			Pthread_cond_wait(&c->empty_cond, &c->queue_mutex);
		}
		pa = c->fullPacketQueue;
		c->fullPacketQueue = pa->next;
		Pthread_mutex_unlock(&c->queue_mutex);

		/* Verarbeite Packet */
		for (k = 0; k < pa->len; ++k)
			ConsumeItem(pa->buf[k], &sum);
		item_cnt += pa->len;
		
		/* Stelle Packet zurueck in emptyPacketQueue des Owners */
		p = producers + pa->owner;
		Pthread_mutex_lock(&p->queue_mutex);
		old = pa->next = p->emptyPacketQueue;
		p->emptyPacketQueue = pa;
		Pthread_mutex_unlock(&p->queue_mutex);
		if (!old)
			Pthread_cond_broadcast(&p->empty_cond);
	}

done:
	c->sum = sum;
	printf("Consumer %d exit, %d items in %d packets received, %d waits\n", 
		c->no, item_cnt, n, waits);
	return NULL;
}