YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
strand.cpp
Go to the documentation of this file.
1/**
2 * \example strand.cpp
3 * Simple \ref Strand examples
4 */
5
11
12#include <cstddef>
13#include <iostream>
14#include <thread>
15#include <vector>
16
17#include <gtest/gtest.h>
18
19namespace test {
20namespace {
21
22TEST(Example, Strand) {
23 std::cout << "Strand" << std::endl;
24
26
27 auto strand = MakeStrand(&tp);
28
29 std::size_t counter = 0;
30
31 static constexpr std::size_t kThreads = 5;
32 static constexpr std::size_t kIncrementsPerThread = 12345;
33
34 std::vector<yaclib_std::thread> threads;
35
36 for (std::size_t i = 0; i < kThreads; ++i) {
37 threads.emplace_back([&]() {
38 for (std::size_t j = 0; j < kIncrementsPerThread; ++j) {
39 Submit(*strand, [&] {
40 ++counter;
41 });
42 }
43 });
44 }
45
46 for (auto& t : threads) {
47 t.join();
48 }
49
50 tp.Stop();
51 tp.Wait();
52
53 std::cout << "Counter value = " << counter << ", expected " << kThreads * kIncrementsPerThread << std::endl;
54
55 std::cout << std::endl;
56}
57
58} // namespace
59} // 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