aboutsummaryrefslogtreecommitdiffstats
path: root/tests/syncqueuetest.cpp
blob: 07167dc60740751ab5d586f19008f176b7840ae9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <yage.h>

#include <atomic>
#include <thread>

using namespace yage;

SyncQueue<int> queue;
std::atomic_int j(0);

void push_to_queue1(int elements)
{
    for (int i = 0; i < elements; i++) {
        queue.push(1);
        j.fetch_add(1, std::memory_order_relaxed);
    }
    std::cout << "Done 1\n";
}

void push_to_queue2(int elements)
{
    for (int i = 0; i < elements; i++) {
        queue.push(2);
        j.fetch_add(1, std::memory_order_relaxed);
    }
    std::cout << "Done 2\n";
}

int main()
{
    std::thread first(push_to_queue1, 100000);
    std::thread second(push_to_queue2, 100000);

    std::cout << "created threads, now adding in main\n";
    for (int i = 0; i < 1000000; ++i) {
        queue.push(i);
        j.fetch_add(1, std::memory_order_relaxed);
    }

    std::cout << "now joining the threads\n";
    first.join();
    second.join();
    std::cout << "done\n"
              << "iterations: " << j << "\n";
}