20 #include <condition_variable> 26 #include <glog/logging.h> 32 TEST(BarrierTest, Simple) {
36 std::condition_variable b1DoneCond;
37 std::condition_variable b2DoneCond;
38 std::atomic<uint32_t> b1TrueSeen(0);
39 std::atomic<uint32_t> b1Passed(0);
40 std::atomic<uint32_t> b2TrueSeen(0);
41 std::atomic<uint32_t> b2Passed(0);
43 Barrier barrier(numThreads + 1);
45 std::vector<std::thread>
threads;
46 threads.reserve(numThreads);
48 threads.emplace_back([&]() {
51 std::unique_lock<std::mutex>
lock(mutex);
53 if (++b1Passed == numThreads) {
54 b1DoneCond.notify_one();
56 return barrier.
wait();
59 std::unique_lock<std::mutex>
lock(mutex);
61 if (++b2Passed == numThreads) {
62 b2DoneCond.notify_one();
70 std::this_thread::sleep_for(std::chrono::milliseconds(50));
74 b1TrueSeen += barrier.
wait().get();
77 std::unique_lock<std::mutex>
lock(mutex);
78 while (b1Passed != numThreads) {
79 b1DoneCond.wait(lock);
85 std::this_thread::sleep_for(std::chrono::milliseconds(50));
89 b2TrueSeen += barrier.
wait().get();
92 std::unique_lock<std::mutex>
lock(mutex);
93 while (b2Passed != numThreads) {
94 b2DoneCond.wait(lock);
99 for (
auto&
t : threads) {
115 static constexpr
uint32_t numIterations = 1;
123 std::vector<uint32_t> trueSeen;
126 std::vector<ThreadInfo>
threads;
127 threads.resize(numThreads);
130 for (
auto& tinfo : threads) {
132 tinfo.trueSeen.resize(numIterations);
133 totalFutures += tinfo.numFutures;
138 for (
auto& tinfo : threads) {
140 tinfo.thread = std::thread([pinfo, &barrier] {
141 std::vector<folly::Future<bool>>
futures;
142 futures.reserve(pinfo->numFutures);
143 for (
uint32_t i = 0;
i < numIterations; ++
i, ++pinfo->iteration) {
145 for (
uint32_t j = 0; j < pinfo->numFutures; ++j) {
146 futures.push_back(barrier.
wait());
149 std::this_thread::sleep_for(std::chrono::nanoseconds(nanos));
152 pinfo->trueSeen[
i] =
std::count(results.begin(), results.end(),
true);
157 for (
auto& tinfo : threads) {
159 EXPECT_EQ(numIterations, tinfo.iteration);
164 for (
auto& tinfo : threads) {
165 trueCount += tinfo.trueSeen[
i];
folly::Future< bool > wait()
#define EXPECT_EQ(val1, val2)
—— Concurrent Priority Queue Implementation ——
std::vector< std::thread::id > threads
auto lock(Synchronized< D, M > &synchronized, Args &&...args)
Future< std::vector< typename std::iterator_traits< InputIterator >::value_type::value_type > > collect(InputIterator first, InputIterator last)
TEST(BarrierTest, Simple)