| 147 | |
| 148 | template<typename T> |
| 149 | static void TestMultipleThreadsCASIncDec() { |
| 150 | thread_group increments, decrements; |
| 151 | vector<int> ops; |
| 152 | ops.push_back(10); |
| 153 | ops.push_back(10000); |
| 154 | vector<int> num_threads; |
| 155 | num_threads.push_back(4); |
| 156 | num_threads.push_back(8); |
| 157 | num_threads.push_back(16); |
| 158 | |
| 159 | for (vector<int>::iterator thrit = num_threads.begin(); thrit != num_threads.end(); |
| 160 | ++thrit) { |
| 161 | for (vector<int>::iterator opit = ops.begin(); opit != ops.end(); ++opit) { |
| 162 | AtomicInt<T> ai(0); |
| 163 | for (int i = 0; i < *thrit; ++i) { |
| 164 | increments.add_thread(new thread(CASIncrementThread<T>, i, *opit, &ai)); |
| 165 | decrements.add_thread(new thread(CASDecrementThread<T>, i, *opit, &ai)); |
| 166 | } |
| 167 | increments.join_all(); |
| 168 | decrements.join_all(); |
| 169 | EXPECT_EQ(ai.Load(), 0); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | TEST(AtomicTest, MultipleThreadsCASIncDec) { |
| 175 | TestMultipleThreadsCASIncDec<int32_t>(); |