-
Notifications
You must be signed in to change notification settings - Fork 7
/
base_atomic.cpp
59 lines (47 loc) · 1.47 KB
/
base_atomic.cpp
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
/*!
* \brief The basic use of atomic.
*/
#include <iostream>
#include <tbb/tbb.h>
class AtomicTest {
public:
AtomicTest(tbb::atomic<int> *a, int *b) : atomic_value_(a), normal_value_(b) {}
void operator () (const tbb::blocked_range<size_t> & r) const {
for(int i=0; i<10000; i++) {
//(*atomic_value_).fetch_and_add(1);
(*atomic_value_)++; // ++ Operator overloading, it is the same as fetch_and_add.
(*normal_value_)++; // Can not get the right result.
}
}
private:
tbb::atomic<int> *atomic_value_;
int *normal_value_;
};
tbb::mutex mutex_lock;
class NormalMutexTest {
public:
NormalMutexTest(int *in) : value_(in) {}
void operator () (const tbb::blocked_range<size_t> & r) const {
tbb::mutex::scoped_lock mylock(mutex_lock);
for (int k = r.begin(); k < r.end(); k++) {
for (int i = 0; i < 10000; i++) {
(*value_)++;
}
}
}
private:
int *value_;
};
int main() {
int num = 500;
tbb::atomic<int> atomic_value = 0;
int normal_value = 0;
int mutex_value = 0;
// ps: tbb::atomic can not get the right answer when the number of threads is too many. (over 500?)
tbb::parallel_for(tbb::blocked_range<size_t>(0, num), AtomicTest(&atomic_value, &normal_value));
tbb::parallel_for(tbb::blocked_range<size_t>(0, num), NormalMutexTest(&mutex_value));
std::cout << "atomic: " << atomic_value << std::endl;
std::cout << "mutex: " << mutex_value << std::endl;
std::cout << "normal: " << normal_value << std::endl;
return 0;
}