16 #include <gtest/gtest.h>
17 #include <boost/thread.hpp>
56 EXPECT_EQ(success,
true);
59 EXPECT_EQ(success,
true);
63 EXPECT_EQ(success,
false);
66 EXPECT_EQ(success,
false);
70 EXPECT_EQ(retval, 100);
73 EXPECT_EQ(retval, 200);
77 EXPECT_EQ(retval, 250);
80 EXPECT_EQ(retval, 250);
82 retval = i1.
Swap(300);
84 EXPECT_EQ(retval, 250);
85 retval = i1.
Swap(350);
87 EXPECT_EQ(retval, 300);
90 TEST(AtomicTest, TestAndSet) {
92 for (
int i = 0; i < 100; ++i) {
98 for (
int i = 0; i < 100; ++i) {
104 typedef function<void (int64_t, int64_t , AtomicInt<int>*)>
Fn;
107 for (int64_t i = 0; i < n * id; ++i) {
113 for (int64_t i = 0; i < n * id; ++i) {
118 TEST(AtomicTest, MultipleThreadsIncDec) {
119 thread_group increments, decrements;
122 ops.push_back(10000);
123 vector<int> num_threads;
124 num_threads.push_back(4);
125 num_threads.push_back(8);
126 num_threads.push_back(16);
128 for (vector<int>::iterator thrit = num_threads.begin(); thrit != num_threads.end();
130 for (vector<int>::iterator opit = ops.begin(); opit != ops.end(); ++opit) {
132 for (
int i = 0; i < *thrit; ++i) {
136 increments.join_all();
137 decrements.join_all();
146 bool success =
false;
147 for (int64_t i = 0; i < n * id; ++i) {
160 bool success =
false;
161 for (int64_t i = 0; i < n * id; ++i) {
171 TEST(AtomicTest, MultipleThreadsCASIncDec) {
172 thread_group increments, decrements;
175 ops.push_back(10000);
176 vector<int> num_threads;
177 num_threads.push_back(4);
178 num_threads.push_back(8);
179 num_threads.push_back(16);
181 for (vector<int>::iterator thrit = num_threads.begin(); thrit != num_threads.end();
183 for (vector<int>::iterator opit = ops.begin(); opit != ops.end(); ++opit) {
185 for (
int i = 0; i < *thrit; ++i) {
189 increments.join_all();
190 decrements.join_all();
198 int main(
int argc,
char **argv) {
199 ::testing::InitGoogleTest(&argc, argv);
200 return RUN_ALL_TESTS();
T UpdateAndFetch(T delta)
Increments by delta (i.e. += delta) and returns the new val.
void CASIncrementThread(int64_t id, int64_t n, AtomicInt< int > *ai)
T Swap(const T &new_val)
Atomically updates value_ with new_val. Returns the old value_.
T FetchAndUpdate(T delta)
Increment by delta and returns the old val.
void UpdateMax(T value)
Updates the int to 'value' if value is larger.
T CompareAndSwapVal(T old_val, T new_val)
T Read()
Safe read of the value.
bool CompareAndSwap(T old_val, T new_val)
Returns true if the atomic compare-and-swap was successful.
void CASDecrementThread(int64_t id, int64_t n, AtomicInt< int > *ai)
function< void(int64_t, int64_t, AtomicInt< int > *)> Fn
int main(int argc, char **argv)
void IncrementThread(int64_t id, int64_t n, AtomicInt< int > *ai)
void DecrementThread(int64_t id, int64_t n, AtomicInt< int > *ai)