| 182 | butil::atomic<int> g_counter(0); |
| 183 | |
| 184 | void* ThreadLocalForEachFunc(void* arg) { |
| 185 | auto counter = static_cast<ThreadLocal<butil::atomic<int>>*>(arg); |
| 186 | auto local_counter = counter->get(); |
| 187 | EXPECT_NE(nullptr, local_counter); |
| 188 | local_counter->store(0, butil::memory_order_relaxed); |
| 189 | while (!g_stopped) { |
| 190 | local_counter->fetch_add(1, butil::memory_order_relaxed); |
| 191 | g_counter.fetch_add(1, butil::memory_order_relaxed); |
| 192 | if (butil::fast_rand_less_than(100) + 1 > 80) { |
| 193 | local_counter = new butil::atomic<int>( |
| 194 | local_counter->load(butil::memory_order_relaxed)); |
| 195 | counter->reset(local_counter); |
| 196 | } |
| 197 | } |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | TEST(ThreadLocalTest, thread_local_for_each) { |
| 202 | g_stopped = false; |