-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLock-free-queue.cpp
More file actions
43 lines (32 loc) · 836 Bytes
/
Lock-free-queue.cpp
File metadata and controls
43 lines (32 loc) · 836 Bytes
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
// Lock-free-queue.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include "LockFreeQueue.h"
using namespace std;
mutex coumtMutex;
void printLine(const string& msg) {
lock_guard<mutex> lock(coumtMutex);
cout << msg << endl;
}
int main()
{
LockFreeQueue<int> q(10);
thread producer([&]() {
for (int i = 0; i < 20; i++) {
while (!q.enqueue(i));
printLine("Produced: " + to_string(i));
}
});
thread consumer([&]() {
for (int i = 0; i < 20; i++) {
int value;
while (!q.dequeue(value));
printLine("Consumed: " + to_string(value));
}
});
producer.join();
consumer.join();
}