forked from artyom-beilis/cppcms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic_test.cpp
More file actions
58 lines (51 loc) · 1.28 KB
/
atomic_test.cpp
File metadata and controls
58 lines (51 loc) · 1.28 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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#include <cppcms/config.h>
#include <booster/thread.h>
#include "atomic_counter.h"
#include "test.h"
#include <iostream>
cppcms::atomic_counter *c;
void inc()
{
for(int i=0;i<1000000;i++)
++*c;
}
void dec()
{
for(int i=0;i<1000000;i++)
--*c;
}
int main()
{
try {
cppcms::atomic_counter counter(0);
TEST(long(counter) == 0);
TEST(++counter == 1);
TEST(--counter == 0);
TEST(++counter == 1);
TEST(long(counter) == 1);
TEST(++counter == 2);
TEST(--counter == 1);
TEST(long(counter) == 1);
TEST(--counter == 0);
TEST(long(counter) == 0);
c=&counter;
booster::thread a(inc);
booster::thread b(dec);
a.join();
b.join();
TEST(long(counter) == 0);
}
catch(std::exception const &e) {
std::cerr << "Failed " << e.what() << std::endl;
return 1;
}
std::cout << "Ok" << std::endl;
return 0;
}