Пример #1
0
int main(int argc, const char* argv[])
{
	queue* q;
	int i;
	pthread_t threads[NUM_THREADS];
	threadinf infos[NUM_THREADS];
	uint64_t thrnums[NUM_THREADS];

	q = queue_create();
	for (i = 0; i < NUM_THREADS; i++)
	{
		thrnums[i] = 1;
		infos[i].thread = i;
		infos[i].q = q;
		pthread_create(&threads[i], NULL, producer, (void *)&infos[i]);
	}

	// for (i = 0; i < ITERATIONS*NUM_THREADS; i++)
	i = 0;
	TIME start;
	GETTIME(start);
	while (i < NUM_THREADS)
	{
		qitem *qi = queue_pop(q);
		item *it = (item*)qi->cmd;
		if (thrnums[it->thread] != it->n)
		{
			printf("Items not sequential thread=%d, n=%llu, shouldbe=%llu, recycled=%u!!\n", 
				it->thread, it->n, thrnums[it->thread], it->recycled);
			return 0;
		}
		thrnums[it->thread]++;
		if (thrnums[it->thread] == ITERATIONS)
			i++;
		// printf("Recycle thr=%d val=%llu, recycled=%u\n",it->thread,it->n, it->recycled++);
		queue_recycle(q,qi);
	}
	uint64_t diff;
	TIME stop;
	GETTIME(stop);
	NANODIFF(stop, start, diff);
	printf("Done in: %llums\n",diff / 1000000);

	// for (i = 0; i < NUM_THREADS; i++)
	// 	printf("threadpos =%llu\n",thrnums[i]);
		// pthread_join(threads[i],NULL);

	// printf("No thread sync errors!\n");
	return 0;
}
Пример #2
0
int main(int argc, const char* argv[])
{
	queue* q;
	int i;
	pthread_t threads[NUM_THREADS];
	threadinf infos[NUM_THREADS];
	int thrnums[NUM_THREADS];

	// less available space than threads
	q = queue_create(NPAGES);
	for (i = 0; i < NUM_THREADS; i++)
	{
		thrnums[i] = 0;
		infos[i].thread = i;
		infos[i].q = q;
		pthread_create(&threads[i], NULL, producer, (void *)&infos[i]);
	}

	for (i = 0; i < ITERATIONS*NUM_THREADS; i++)
	{
		qitem *qi = queue_pop(q);
		item *it = (item*)qi->cmd;
		// if (thrnums[it->thread] != it->n)
		// {
		// 	printf("Items not sequential thread=%d, n=%d, shouldbe=%d!!\n", it->thread, it->n, thrnums[it->thread]);
		// 	return 0;
		// }
		thrnums[it->thread]++;
		queue_recycle(q,qi);
		// printf("Recycle %d\n",i);
	}

	// for (i = 0; i < NUM_THREADS; i++)
	// 	pthread_join(threads[i],NULL);

	// printf("No thread sync errors!\n");
	return 0;
}