forked from tempesta-tech/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcc-stm.cc
executable file
·57 lines (49 loc) · 1.08 KB
/
gcc-stm.cc
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
/**
* Simple threading test program for GCC-4.7 Software Transactional Memory.
* Compile with -std=c++11 and -fgnu-tm.
*
* Written by Alexander Krizhanovsky ([email protected]).
*/
#include <iostream>
#include <thread>
static const auto THR_NUM = 4;
static const auto ITER_NUM = 1000 * 1000;
static auto a = 0, b = 0, c = 0, d = 0;
static void __attribute__((noinline))
thr_func()
{
try {
for (auto i = 0; i < ITER_NUM; ++i)
__transaction_atomic {
++a;
auto e = b + 2;
b += 2;
c = a + b;
b = e;
if (a == 1) {
// It seems the transaction is still
// commited, sonce d=1 at the end
// of program.
d = 1;
throw std::exception();
}
}
}
catch (std::exception &e) {
std::cout << e.what() << std::endl;
}
}
int
main(int argc, char *argv[])
{
std::thread thr[THR_NUM];
for (auto &t : thr)
t = std::thread(thr_func);
for (auto &t : thr)
t.join();
std::cout << "a=" << a << " b=" << b << " c=" << c
<< " d=" << d << std::endl;
std::cout << "addr_a=" << &a << " addr_b=" << &b
<< " addr_c=" << &c << std::endl;
return 0;
}