1 // ============================================================================
2 /// @file test_blocking_q.cpp
3 /// @brief Benchmark blocking queue
4 // ============================================================================
8 #include <glib.h> // GTimeVal + g_get_current_time
9 #include <omp.h> // parallel processing support in gcc
10 #include "g_blocking_queue.h"
21 #define N_ITERATIONS 10000000
25 #define QUEUE_SIZE 1000
28 void TestBlockingQueue()
30 BlockingQueue<int> theQueue(QUEUE_SIZE);
31 GTimeVal iniTimestamp;
32 GTimeVal endTimestamp;
34 std::cout << "=== Start of testing blocking queue ===" << std::endl;
35 g_get_current_time(&iniTimestamp);
36 #pragma omp parallel shared(theQueue) num_threads (2)
38 if (omp_get_thread_num() == 0)
40 if (!omp_get_nested())
42 std::cerr << "WARNING: Nested parallel regions not supported. Working threads might have unexpected behaviour" << std::endl;
43 std::cerr << "Are you running with \"OMP_NESTED=TRUE\"??" << std::endl;
47 #pragma omp sections //nowait
52 #pragma omp parallel shared(theQueue) num_threads (N_PRODUCERS)
55 #pragma omp for schedule(static) private(i) nowait
56 for (i = 0 ; i < N_ITERATIONS ; i++)
58 while(!theQueue.Push(i))
69 #pragma omp parallel shared(theQueue) num_threads (N_CONSUMERS)
73 #pragma omp for schedule(static) private(i, result) nowait
74 for (i = 0 ; i < N_ITERATIONS ; i++)
76 // this call will block if the queue is empty until
77 // some other thread pushes something into it
84 } // #pragma omp parallel
86 g_get_current_time(&endTimestamp);
88 // calculate elapsed time
90 if (endTimestamp.tv_usec >= iniTimestamp.tv_usec)
92 elapsedTime.tv_sec = endTimestamp.tv_sec - iniTimestamp.tv_sec;
93 elapsedTime.tv_usec = endTimestamp.tv_usec - iniTimestamp.tv_usec;
97 elapsedTime.tv_sec = endTimestamp.tv_sec - iniTimestamp.tv_sec - 1;
98 elapsedTime.tv_usec = G_USEC_PER_SEC + endTimestamp.tv_usec - iniTimestamp.tv_usec;
101 std::cout << "Elapsed: " << elapsedTime.tv_sec << "." << elapsedTime.tv_usec << std::endl;
102 std::cout << "=== End of testing blocking queue ===" << std::endl;
105 int main(int /*argc*/, char** /*argv*/)
108 std::cout << "Done!!!" << std::endl;