| 83 | |
| 84 | template<typename T> |
| 85 | static void TestMultipleThreadsIncDec() { |
| 86 | thread_group increments, decrements; |
| 87 | vector<int> ops; |
| 88 | ops.push_back(1000); |
| 89 | ops.push_back(10000); |
| 90 | vector<int> num_threads; |
| 91 | num_threads.push_back(4); |
| 92 | num_threads.push_back(8); |
| 93 | num_threads.push_back(16); |
| 94 | |
| 95 | for (vector<int>::iterator thrit = num_threads.begin(); thrit != num_threads.end(); |
| 96 | ++thrit) { |
| 97 | for (vector<int>::iterator opit = ops.begin(); opit != ops.end(); ++opit) { |
| 98 | AtomicInt<T> ai(0); |
| 99 | for (int i = 0; i < *thrit; ++i) { |
| 100 | increments.add_thread(new thread(IncrementThread<T>, i, *opit, &ai)); |
| 101 | decrements.add_thread(new thread(DecrementThread<T>, i, *opit, &ai)); |
| 102 | } |
| 103 | increments.join_all(); |
| 104 | decrements.join_all(); |
| 105 | EXPECT_EQ(ai.Load(), 0); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | TEST(AtomicTest, MultipleThreadsIncDec) { |
| 111 | TestMultipleThreadsIncDec<int32_t>(); |