-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
68 lines (62 loc) · 1.85 KB
/
example.cpp
File metadata and controls
68 lines (62 loc) · 1.85 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "msqueue.h"
#include <iostream>
int main() {
unsigned int var = 1;
unsigned int to_cmp = 1;
if (htm_compare_and_swap(&var, &to_cmp,(unsigned int)5)) {
std::cout << "Success: " << var << std::endl;
} else {
std::cout << "Failed: " << var << std::endl;
}
unsigned int test_var = 1;
unsigned int wrong_to_cmp = 2;
int failures = 0;
int successes = 0;
for (int i = 0; i < 100; i++) {
if (htm_compare_and_swap(&test_var, &to_cmp, (unsigned int) 1)) {
successes++;
} else {
failures ++;
}
if (!htm_compare_and_swap(&test_var, &wrong_to_cmp, (unsigned int) 1)) {
successes++;
} else {
failures ++;
}
}
std::cout << "Success: " << successes << std::endl << "Failures: " << failures << std::endl;
{
HTM_CAS_MSQueue<unsigned int> queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
unsigned int outValue = 0;
queue.dequeue(&outValue);
std::cout << outValue << std::endl;
if (outValue != 1) {
std::cerr << "outValue != 1" << std::endl;
}
queue.dequeue(&outValue);
std::cout << outValue << std::endl;
if (outValue != 2) {
std::cerr << "outValue != 1" << std::endl;
}
}
{
STD_CAS_MSQueue<unsigned int> queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
unsigned int outValue = 0;
queue.dequeue(&outValue);
std::cout << outValue << std::endl;
if (outValue != 1) {
std::cerr << "outValue != 1" << std::endl;
}
queue.dequeue(&outValue);
std::cout << outValue << std::endl;
if (outValue != 2) {
std::cerr << "outValue != 1" << std::endl;
}
}
}