Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "absl/synchronization/blocking_counter.h"
00016
00017 #include <thread>
00018 #include <vector>
00019
00020 #include "gtest/gtest.h"
00021 #include "absl/time/clock.h"
00022 #include "absl/time/time.h"
00023
00024 namespace absl {
00025 namespace {
00026
00027 void PauseAndDecreaseCounter(BlockingCounter* counter, int* done) {
00028 absl::SleepFor(absl::Seconds(1));
00029 *done = 1;
00030 counter->DecrementCount();
00031 }
00032
00033 TEST(BlockingCounterTest, BasicFunctionality) {
00034
00035
00036
00037
00038 const int num_workers = 10;
00039 BlockingCounter counter(num_workers);
00040
00041 std::vector<std::thread> workers;
00042 std::vector<int> done(num_workers, 0);
00043
00044
00045
00046 workers.reserve(num_workers);
00047 for (int k = 0; k < num_workers; k++) {
00048 workers.emplace_back(
00049 [&counter, &done, k] { PauseAndDecreaseCounter(&counter, &done[k]); });
00050 }
00051
00052
00053 counter.Wait();
00054
00055
00056 for (int k = 0; k < num_workers; k++) {
00057 EXPECT_EQ(1, done[k]);
00058 }
00059
00060 for (std::thread& w : workers) {
00061 w.join();
00062 }
00063 }
00064
00065 }
00066 }