SObjectizer  5.8
Loading...
Searching...
No Matches
abstract_work_thread.cpp
Go to the documentation of this file.
1/*
2 * SObjectizer-5
3 */
4
5/*!
6 * \file
7 * \brief Interfaces for work_thread and work_thread's factory.
8 * \since v.5.7.3
9 */
10
11#include <so_5/disp/abstract_work_thread.hpp>
12
13#include <so_5/details/suppress_exceptions.hpp>
14
15#include <thread>
16
17namespace so_5::disp
18{
19
20//
21// abstract_work_thread_t
22//
25
28
29//
30// abstract_work_thread_factory_t
31//
36
38{
39
40//
41// std_work_thread_t
42//
43/*!
44 * \brief The standard implementation of abstract_work_thread interface.
45 *
46 * It uses std::thread without any additiona tuning.
47 * An actual instance of thread is created in start() and then joined only
48 * in join().
49 *
50 * \note
51 * This implementation assumes that start() will be called before join(), and if
52 * start() was called then someone must call join() before the destruction
53 * of the object.
54 *
55 * \since v.5.7.3
56 */
58 {
59 //! Actual thread.
61
62 public:
63 std_work_thread_t() = default;
64
65 void
66 start( body_func_t thread_body ) override
67 {
68 m_thread = std::thread{
69 [tb = std::move(thread_body)] {
70 // All exceptions have to be intercepted and suppressed.
71 so_5::details::suppress_exceptions( [&tb]() { tb(); } );
72 }
73 };
74 }
75
76 void
77 join() override
78 {
79 // Do not check m_thread status because join() has to be called
80 // only once and only if there was previous call to start(),
81 // and that call was successful.
82 m_thread.join();
83 }
84 };
85
86//
87// std_work_thread_factory_t
88//
89/*!
90 * \brief The standard implementation of abstract_work_thread_factory interface.
91 *
92 * This implementation creates a new instance of std_work_thread_t dynamically
93 * in every call to acquire(). An instance of std_work_thread_t is expected
94 * in release() (but it's not checked) and the instance passed to
95 * release() is just deleted by using ordinary `delete`.
96 *
97 * \since v.5.7.3
98 */
100 {
101 public:
103
104 [[nodiscard]]
106 acquire( so_5::environment_t & /*env*/ ) override
107 {
108 return *(new std_work_thread_t{});
109 }
110
111 void
112 release( abstract_work_thread_t & thread ) noexcept override
113 {
114 // Assume that 'thread' was created via acquire() method,
115 // so we can safely delete it.
116 delete (&thread);
117 }
118 };
119
120} /* namespace std_work_thread_impl */
121
122//
123// make_std_work_thread_factory
124//
125[[nodiscard]]
129{
130 return std::make_shared<
131 std_work_thread_impl::std_work_thread_factory_t >();
132}
133
134} /* namespace so_5::disp */
An interface of factory for management of worker threads.
An interface for one worker thread.
The standard implementation of abstract_work_thread_factory interface.
abstract_work_thread_t & acquire(so_5::environment_t &) override
Get a new worker thread from factory.
void release(abstract_work_thread_t &thread) noexcept override
Return a worker thread back to the factory.
The standard implementation of abstract_work_thread interface.
void join() override
Stops the current thread until worker thread completes execution of thread_body passed to previous ca...
void start(body_func_t thread_body) override
Start a new thread and execute specified functor on it.
SObjectizer Environment.
#define SO_5_FUNC
Definition declspec.hpp:48
Event dispatchers.
SO_5_FUNC abstract_work_thread_factory_shptr_t make_std_work_thread_factory()
Get a standard SObjectizer's work thread factory that is used by default.
Private part of message limit implementation.
Definition agent.cpp:33