1 #include <cds/container/rwqueue.h>
9 cds::container::RWQueue<int> queue;
12 for (int i = 0; i < 2000000; i++) {
13 queue.enqueue(rand() % 100);
17 void ProducerThread() {
18 for (int i = 0; i < 1000000; i++) {
19 for (int j = 0; j < 50; j++) {
20 queue.enqueue(rand() % 100);
25 void ProducerConsumerThread() {
26 unsigned long long sum = 0;
28 for (int i = 0; i < 1000000; i++) {
29 for (int j = 0; j < 50; j++) {
30 if (!queue.empty() && queue.dequeue(element)) {
34 queue.enqueue(rand() % 100);
40 void ConsumerThread() {
42 unsigned long long sum = 0;
44 while (yield_times > 0) {
45 while (queue.dequeue(element)) {
49 std::this_thread::yield();
56 const int kThreads = 6;
57 // Initialize the queue with some elements.
59 cout << "Starting " << kThreads << " threads for RWQueue...\n";
61 struct timespec start, finish;
63 clock_gettime(CLOCK_MONOTONIC, &start);
65 std::thread threads[kThreads];
67 threads[0] = std::thread(ProducerThread);
68 // ProducerConsumer threads
69 for (int i = 1; i < kThreads; i++) {
70 threads[i] = std::thread(ProducerConsumerThread);
73 for (int i = 0; i < kThreads; i++) {
77 clock_gettime(CLOCK_MONOTONIC, &finish);
78 elapsed = (finish.tv_sec - start.tv_sec);
79 elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
80 cout << "All threads finished.\n";
81 cout << "Time: " << elapsed << " seconds\n";