YACLib
C++ library for concurrent tasks execution
strand.cpp

Simple Strand examples.

Simple Strand examples

/**
* \example strand.cpp
* Simple \ref Strand examples
*/
#include <cstddef>
#include <iostream>
#include <thread>
#include <vector>
#include <gtest/gtest.h>
namespace test {
namespace {
TEST(Example, Strand) {
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;
}
} // namespace
} // namespace test
TODO(kononovk) Doxygen docs.
void Submit(IExecutor &executor, Func &&f)
Submit given func for details.
Definition: submit.hpp:17
IExecutorPtr MakeStrand(IExecutorPtr e)
Strand is the asynchronous analogue of a mutex.
Definition: strand.cpp:71
TEST(Example, HelloWorld)
Definition: simple.cpp:27