Exemple #1
0
static void
test_Queue()
{
	Queue* queue = Queue_new();

	Consumer* consumers[NTHREADS_TOTAL];

	int i_thread;

	for (i_thread = 0; i_thread < (NTHREADS_TOTAL / 2); i_thread++)
		consumers[i_thread] = Consumer_start(queue, i_thread, get_item);

	for (; i_thread < NTHREADS_TOTAL; i_thread++)
		consumers[i_thread] = Consumer_start(queue, i_thread, get_item_timed);

	PROFILE_BEGIN_FMT("Processing %d simple requests with %d threads\n",
			  NITERATATIONS, NTHREADS_TOTAL);

	int i_item;
	int expected_sum = 0;

	for (i_item = 0; i_item < NITERATATIONS; i_item++)
	{
		int* x = cx_alloc(sizeof(int));
		*x = i_item;
		Queue_add(queue, x);
		/* simulate submission delay */

		expected_sum += i_item;

#ifdef SUBMISSION_DELAY_MAX_MSEC
		usleep((rand() % SUBMISSION_DELAY_MAX_MSEC) * 1000);
#endif
	}

	/* wait for threads to finish processing */
	int total_processed = 0;
	for (i_thread = 0; i_thread < NTHREADS_TOTAL; i_thread++)
	{
		Consumer* consumer = consumers[i_thread];
		// see http://stackoverflow.com/questions/5610677/valgrind-memory-leak-errors-when-using-pthread-create
		//	http://stackoverflow.com/questions/5282099/signal-handling-in-pthreads
		pthread_join(*consumer->thread, NULL);
		XFLOG("Consumer[%d] processed %d", consumer->id, consumer->processed);
		total_processed += consumer->processed;
		Consumer_free(consumer);
	}

	PROFILE_END

	TEST_ASSERT_EQUAL(total_processed, NITERATATIONS);
}
int main(void) {
    srand(time(NULL));
    stack_t stack = stack_new();
    hMutex = CreateMutex(
        NULL,
        FALSE,
        NULL);
    HANDLE * first_thread = Producer_new(stack);
    HANDLE * second_thread = Consumer_new(stack);
    while (!_kbhit());
    Producer_free(first_thread);
    Consumer_free(second_thread);
    CloseHandle(hMutex);
    _getch();
    system("cls");
    return 0;
}