YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
strand.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <yaclib/exe/job.hpp>
5
6#include <yaclib_std/atomic>
7
8namespace yaclib {
9
10class Strand : private Job, public IExecutor {
11 // Inheritance from two IRef's, but that's okay, because they are pure virtual
12 public:
13 explicit Strand(IExecutorPtr e) noexcept;
14
15 ~Strand() noexcept override;
16
17 [[nodiscard]] Type Tag() const noexcept final;
18
19 [[nodiscard]] bool Alive() const noexcept final;
20
21 void Submit(Job& job) noexcept final;
22
23 private:
24 void Call() noexcept final;
25
26 void Drop() noexcept final;
27
28 Node* Mark() noexcept;
29
30 IExecutorPtr _executor;
31 yaclib_std::atomic<Node*> _jobs{Mark()};
32};
33
34/**
35 * Strand is the asynchronous analogue of a mutex
36 *
37 * It guarantees that the tasks scheduled for it will be executed strictly sequentially.
38 * Strand itself does not have its own threads, it decorates another executor and uses it to run its tasks.
39 * \param e executor to decorate
40 * \return pointer to new Strand instance
41 */
43
44} // namespace yaclib
Callable that can be executed in an IExecutor.
Definition job.hpp:12
bool Alive() const noexcept final
Return true if executor still alive, that means job passed to submit will be Call.
Definition strand.cpp:20
Type Tag() const noexcept final
Return type of this executor.
Definition strand.cpp:16
void Submit(Job &job) noexcept final
Submit given job.
Definition strand.cpp:24
~Strand() noexcept override
Definition strand.cpp:12
IExecutorPtr MakeStrand(IExecutorPtr e)
Strand is the asynchronous analogue of a mutex.
Definition strand.cpp:71
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25