Simple Strand examples.
Simple Strand examples
#include <cstddef>
#include <iostream>
#include <thread>
#include <vector>
#include <gtest/gtest.h>
namespace {
std::cout << "Strand" << std::endl;
auto strand = MakeStrand(&tp);
std::size_t counter = 0;
static constexpr std::size_t kThreads = 5;
static constexpr std::size_t kIncrementsPerThread = 12345;
std::vector<yaclib_std::thread> threads;
for (std::size_t i = 0; i < kThreads; ++i) {
threads.emplace_back([&]() {
for (std::size_t j = 0; j < kIncrementsPerThread; ++j) {
Submit(*strand, [&] {
++counter;
});
}
});
}
for (auto& t : threads) {
t.join();
}
tp.Stop();
tp.Wait();
std::cout << "Counter value = " << counter << ", expected " << kThreads * kIncrementsPerThread << std::endl;
std::cout << std::endl;
}
}
}
TODO(kononovk) Doxygen docs.
TEST(Example, HelloWorld)