{"id":471176,"date":"2025-08-18T21:00:51","date_gmt":"2025-08-18T21:00:51","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=471176"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=471176","title":{"rendered":"<span>\u0412\u043e\u043b\u0448\u0435\u0431\u0441\u0442\u0432\u043e \u041e\u041e\u041f \u0438\u043b\u0438 \u043a\u0430\u043a \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 C++<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430\u0447\u043d\u0451\u043c \u043c\u044b \u0441 \u043f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0437\u0430\u0434\u0430\u0447\u0438: \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043d\u0435\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0438\u043c\u0435\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u0434\u043b\u044f \u0435\u0451 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u0442\u0430\u0442\u0443\u0441\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \/ \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438 \u043f\u0440\u043e\u0447\u0435\u0435. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043a\u043b\u0430\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">using namespace std;  class Object \/\/ \u043d\u0430\u0448 \u043d\u0435\u043a\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446 \u043c\u0435\u0442\u043e\u0434\u0430 { public:     Object() : \/\/ \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043d\u0438\u0447\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e         FuncThread(nullptr),         FuncStatus(false)     {}     ~Object() \/\/ \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440     {         StopFunc(); \/\/ \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u0438     }     void Func() \/\/ \u0441\u0430\u043c\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a     {         while (FuncStatus)         {             \/\/Do something         }     }     bool StartFunc() \/\/ \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0435\u0451 \u0437\u0430\u043f\u0443\u0441\u043a\u0430     {         if (FuncThread || FuncStatus)             return false;         FuncThread = new thread(&amp;Object::Func, this);         FuncStatus = true;         return true;     }     bool StopFunc() \/\/ \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0435\u0451 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438     {         if (!FuncThread &amp;&amp; !FuncStatus)             return false;         FuncStatus = false;         if (FuncThread &amp;&amp; FuncThread-&gt;joinable()) FuncThread-&gt;join();         if (FuncThread) delete FuncThread;         FuncThread = nullptr;         return true;     }     bool Status() { return FuncStatus; } \/\/ \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0435\u0451 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f  private:     thread* FuncThread; \/\/ \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u043e\u0442\u043e\u043a, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043a\u0440\u0443\u0442\u0438\u0442\u044c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f     mutex FuncMutex; \/\/ \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043b\u044f \u043e\u0431\u043c\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0441 \u043f\u043e\u0442\u043e\u043a\u043e\u043c     atomic&lt;bool&gt; FuncStatus; \/\/ \u0441\u0442\u0430\u0442\u0443\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043e\u043d \u0436\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0444\u043b\u0430\u0433 }; <\/code><\/pre>\n<p>\u041d\u0430\u0434\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e <code>mutex<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438, \u0434\u043b\u044f \u043e\u0431\u043c\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430 <code>FuncStatus<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u044b\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0443\u0441\u0430 \u0440\u0430\u0431\u043e\u0442\u044b.<br \/> \u041d\u0443 \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0435-\u0442\u0440\u0438 \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435. \u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430? \u0427\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0437\u0432\u0443\u0447\u0438\u0442 \u043a\u0430\u043a \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0433\u043e\u043b\u043e\u0432\u043d\u0430\u044f \u0431\u043e\u043b\u044c.<br \/> \u041c\u043d\u043e\u044e \u0438 \u043c\u043e\u0438\u043c \u043a\u043e\u043b\u043b\u0435\u0433\u043e\u0439 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u043a\u0443\u044e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u0443\u044e \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0434\u043b\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0438 \u043c\u044b \u043f\u0435\u0440\u0435\u0448\u043b\u0438 \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u043d\u0435\u0451 \u0436\u0435<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0432\u043e\u0434 \u043e \u0435\u0451 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0432 \u0441\u0442\u0430\u0442\u0443\u0441<\/p>\n<\/li>\n<li>\n<p>\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0441\u0440\u0430\u0437\u0443 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u0438 \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432.<\/p>\n<h2>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 <code>type_traits<\/code>, \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0448 \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0438, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043d\u0451\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u043c\u043e\u0449\u043d\u0435\u0439\u0448\u0438\u0435 \u0438, \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f, \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043e\u0431\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u0441\u0442\u0432 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u043f\u0440\u043e\u0447\u0435\u0433\u043e.<\/p>\n<pre><code class=\"cpp\">\/\/ Function template&lt;typename Callable&gt; struct callable_decay {     using return_type = std::invoke_result_t&lt;Callable&gt;;     using owner_type  = void; }; \/\/ Function with args template&lt;typename Return, typename... Args&gt; struct callable_decay&lt;Return(Args...)&gt; {     using return_type = Return;     using owner_type  = void; }; \/\/ Member pointer function template&lt;typename Return, typename Class, typename... Args&gt; struct callable_decay&lt;Return(Class ::*)(Args...)&gt;  {     using return_type = Return;     using owner_type  = Class; }; \/\/ Const member pointer function template&lt;typename Return, typename Class, typename... Args &gt; struct callable_decay&lt;Return(Class::*)(Args...) const&gt;  {     using return_type = Return;     using owner_type  = Class; };  template&lt;typename Callable&gt; using callable_return_type_t = typename callable_decay&lt;Callable&gt;::return_type; template&lt;typename Callable&gt; using callable_owner_type_t  = typename callable_decay&lt;Callable&gt;::owner_type; <\/code><\/pre>\n<p>\u041c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>callable_decay<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0442\u0438\u043f &#171;\u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430&#187; \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u043c\u0435\u0442\u043e\u0434. \u0422\u0430\u043a\u0436\u0435 \u043e\u0431\u044a\u044f\u0432\u0438\u043c <code>callable_return_type_t<\/code> \u0438 <code>callable_owner_type_t <\/code> \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448 \u043a\u043e\u0434 \u043d\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0442\u0430\u043a \u0441\u0442\u0440\u0430\u0448\u043d\u043e \u0438 \u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043e.<\/p>\n<pre><code class=\"cpp\">\/\/ Threaded func possible states enum ThreadedFuncStatus_T {     no_process,     is_starting,     is_running,     is_stopping }; <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0434\u043b\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<ol>\n<li>\n<p>\u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442<\/p>\n<\/li>\n<li>\n<p>\u0441\u0442\u0430\u0440\u0442\u0443\u0435\u043c<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u0441\u044f<\/p>\n<\/li>\n<li>\n<p>\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c\u0441\u044f<\/p>\n<\/li>\n<\/ol>\n<h2>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043b\u0430\u0441\u0441<\/h2>\n<h3>\u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435<\/h3>\n<pre><code class=\"cpp\">\/\/ function_type - \u0442\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043e\u0431\u0451\u0440\u0442\u043a\u0435 \/\/ function_owner_type  - \u0442\u0438\u043f \"\u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430\" \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043a\u043b\u0430\u0441\u0441\u0430) \/\/ handler_type - \u0442\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 template&lt;typename function_type,           typename function_owner_type = std::nullptr_t,           typename handler_type        = std::nullptr_t&gt; class ThreadedFunc { public: \/\/ \u0412\u044b\u044f\u0441\u043d\u044f\u0435\u043c \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f using function_return_type = callable_return_type_t&lt;function_type&gt;;      \/\/ \u0410 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u043b \u043b\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u043d\u0430\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u043e\u0442 \u0436\u0435 \u0442\u0438\u043f \"\u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430\" \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0447\u0442\u043e \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c \u0441\u0430\u043c\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f     static_assert(!std::is_member_function_pointer_v&lt;function_type&gt; ||          std::is_same_v&lt;callable_owner_type_t&lt;function_type&gt;, function_owner_type&gt;,         \"Invalid function owner type\");      \/\/ \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440     ThreadedFunc(function_type function, function_owner_type*     owner = nullptr, handler_type handler_pred = nullptr);     \/\/ \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u043d\u043e\u0432\u043e\u0433\u043e \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0433\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u044f     ThreadedFunc(ThreadedFunc&amp;&amp; other,   function_owner_type* new_owner = nullptr);     \/\/ \u0410 \u0432\u043e\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u044b \u0443\u0434\u0430\u043b\u044f\u0435\u043c     ThreadedFunc(const ThreadedFunc&amp;) = delete;     \/\/ \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043d\u0438\u0447\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e     ~ThreadedFunc();      \/\/ \u0422\u0430\u043a\u0436\u0435 \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u044f     ThreadedFunc&amp; operator=(const ThreadedFunc&amp;) = delete;      \/\/ \u041c\u0435\u0442\u043e\u0434\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430, \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0443\u0441\u0430     inline bool Run();     inline bool Stop();     inline bool StopAsync();     inline void WaitUntilStop() const;     inline bool Status() const;  private:     \/\/ \u0425\u0438\u0442\u0440\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438     inline void StopLoop();     \/\/ \u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \"\u0445\u0440\u0430\u043d\u0438\u043c\u043e\u0439\" \u0444\u0443\u043d\u043a\u0446\u0438\u0438     inline function_return_type CallStoredFunction();     \/\/ \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u0430     void MainLoop();      function_type  StoredFunction;  \/\/ \u0425\u0440\u0430\u043d\u0438\u043c\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f     function_owner_type* const  StoredObjectPtr; \/\/ \u0425\u0440\u0430\u043d\u0438\u043c\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \"\u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430\"     handler_type  StoredHandler;   \/\/ \u0425\u0440\u0430\u043d\u0438\u043c\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a      std::atomic&lt;ThreadedFuncStatus_T&gt; TFStatus; \/\/ \u041d\u0430\u0448 \u0441\u0442\u0430\u0442\u0443\u0441     std::thread  MainThread; \/\/ \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0441\u0430\u043c \u043f\u043e\u0442\u043e\u043a }; <\/code><\/pre>\n<p>\u041f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432, \u0432\u0441\u0451 \u0442\u0430\u043a\u0438 \u043f\u043e\u044f\u0441\u043d\u044e \u043f\u0430\u0440\u0443 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432.<br \/> \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, <code>static_assert<\/code> \u043a\u0440\u0430\u0439\u043d\u0435 \u0432\u0430\u0436\u0435\u043d, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0435\u0434\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u043d\u0430\u0441 \u0441\u0430\u043c\u0438\u0445 \u0436\u0435 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043e\u0442 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u043e\u0431\u0451\u0440\u0442\u043a\u0443, \u0447\u0442\u043e\u0431\u044b \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043d\u0435 \u0438\u0441\u043a\u0430\u0442\u044c \u0433\u0434\u0435 \u0438 \u043a\u0430\u043a \u043c\u044b \u043e\u043f\u043b\u043e\u0448\u0430\u043b\u0438.<br \/> \u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u043e\u0431\u0440\u0430\u0449\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u044b <code>StopAsync()<\/code> \u0438 <code>WaitUntilStop()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u043e\u0431\u0451\u0440\u0442\u043a\u0438, \u0434\u0430\u0436\u0435 \u0438\u0437 \u0441\u0430\u043c\u043e\u0439 \u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0438! \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043e\u0442\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0442\u043e\u0447\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438.<br \/> \u0412-\u0442\u0440\u0435\u0442\u044c\u0438\u0445, \u0441\u0442\u043e\u0438\u0442 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e \u043d\u0435\u043a\u0438\u0439 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043e \u043d\u0451\u043c \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043d\u0438\u0436\u0435, \u0430 \u043f\u043e\u043a\u0430 \u0441\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0433\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u043c\u044b \u043d\u0435 \u0437\u0430\u0432\u044f\u0437\u0430\u043d\u044b \u043d\u0430 \u0435\u0433\u043e \u0442\u0438\u043f.<br \/> \u041d\u0443 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435, \u0447\u0442\u043e \u044f \u0437\u0430\u043c\u0435\u0447\u0443 &#8212; \u044d\u0442\u043e \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u044f, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0442\u0430\u043a \u0438 \u043d\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u0438\u0439\u0442\u0438 \u043a \u0435\u0434\u0438\u043d\u043e\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u0432 \u044d\u0442\u0438\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445, \u043e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435.<\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<h4>CallStoredFunction()<\/h4>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u0437\u0430\u0433\u0430\u0434\u043e\u0447\u043d\u043e\u0433\u043e \u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e &#8212; \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u0445\u0440\u0430\u043d\u0438\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<pre><code class=\"cpp\">inline function_return_type CallStoredFunction()  {     if constexpr (std::is_member_function_pointer_v&lt;function_type&gt;)         return (StoredObjectPtr-&gt;*StoredFunction)();     else         return (*StoredFunction)(); } <\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u043d\u0435\u0442, \u043e\u0434\u043d\u0430\u043a\u043e <code>constexpr<\/code> \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0438 \u0440\u0430\u0437\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u043c\u0430\u0448\u0438\u043d\u0443 \u043e\u0442 \u043b\u0438\u0448\u043d\u0438\u0445 \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u0439 \u043e \u0442\u0438\u043f\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0435\u0449\u0451 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0432\u0435\u0434\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f-\u0447\u043b\u0435\u043d \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u0441\u0442\u0430\u0442\u044c \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0439 \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442.<\/p>\n<h4>MainLoop()<\/h4>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0443\u0436\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c, \u043f\u043e\u0440\u0430 \u0431\u044b \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430<\/p>\n<pre><code class=\"cpp\">void MainLoop() {     TFStatus = ThreadedFuncStatus_T::is_running;     bool handler_error = false;      while (true)     {         if (TFStatus == ThreadedFuncStatus_T::is_stopping || handler_error)             break;          if constexpr (std::is_same_v&lt;function_return_type, void&gt;)             \/\/ no return type             CallStoredFunction();         else if constexpr (std::is_same_v&lt;handler_type, bool(*)(function_return_type)&gt;)             \/\/ a handler is presented             handler_error = (*StoredHandler)(CallStoredFunction());         else             \/\/ no handler - treating return type as bool             handler_error = static_cast&lt;bool&gt;(CallStoredFunction());     } } <\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0441 \u0442\u0435\u043c \u0436\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0441\u043b\u043e\u0432\u043e\u043c <code>constexpr<\/code> \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u0442\u0440\u0438 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u0451\u0440\u0442\u043a\u0438:<\/p>\n<ol>\n<li>\n<p>\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0438 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0451 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c<\/p>\n<\/li>\n<li>\n<p>\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a (\u043f\u0440\u0438 \u0435\u0433\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0438) \u0438 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0432\u043e\u0434 \u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u044f \u0443\u0436\u0435 \u0438\u0437 \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0430\u043c \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043d\u0430\u043c, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u0431\u0435\u0437 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0438 \u0432\u0441\u0451\u043c \u044d\u0442\u043e\u043c \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0444\u043b\u0430\u0433 \u0441\u0442\u0430\u0442\u0443\u0441\u0430 \u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043b\u0438 \u0434\u0430\u043b\u044c\u0448\u0435 \u043a\u0440\u0443\u0442\u0438\u0442\u044c\u0441\u044f \u0438\u043b\u0438 \u043f\u043e\u0440\u0430 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u0438\u0437-\u0437\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438\u0437\u0432\u043d\u0435.<\/p>\n<h4>\u041f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e\u0431 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435<\/h4>\n<p>\u0421\u0442\u043e\u0438\u0442 \u0432\u0441\u0451 \u0442\u0430\u043a\u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e\u0431 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435. \u0414\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043c\u044b \u0441 \u043a\u043e\u043b\u043b\u0435\u0433\u043e\u0439 \u0442\u0430\u043a \u0438 \u043d\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u0438\u0439\u0442\u0438 \u043a \u0435\u0434\u0438\u043d\u043e\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e \u043e\u0431 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u044d\u0442\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430, \u043d\u043e \u0432\u0441\u0451 \u0436\u0435 \u0440\u0435\u0448\u0438\u043b\u0438, \u0447\u0442\u043e \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u043d\u0435\u043a\u043e\u0433\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u0432\u0438\u0434\u0435 \u043e\u0448\u0438\u0431\u043e\u043a (\u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 <code>boost::system::error_code<\/code> \u0438 \u0438\u043c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u043c), \u0431\u044b\u043b\u043e \u0431\u044b \u0437\u0434\u043e\u0440\u043e\u0432\u043e \u0438\u043c\u0435\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\/\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a \u0442\u0430\u043a\u0438\u0445 \u0432\u043e\u0442 \u043e\u0448\u0438\u0431\u043e\u043a \u043a \u043f\u0440\u043e\u0441\u0442\u043e\u043c\u0443 \u0432\u044b\u0432\u043e\u0434\u0443: \u0441\u0442\u043e\u0438\u0442 \u043b\u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0438\u043b\u0438 \u043c\u044b \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0438\u0441\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u043e\u0439.<br \/> \u0414\u0430, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u044d\u0442\u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u043b\u043e\u0436\u0435\u043d \u0441\u0440\u0430\u0437\u0443 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043e\u0431\u0451\u0440\u0442\u043a\u0443, \u043d\u043e \u0440\u0430\u0437\u0432\u0435 \u0442\u0430\u043a \u0431\u044b\u043b\u043e \u0431\u044b \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435?<\/p>\n<h4>StopLoop()<\/h4>\n<p>\u0422\u0430 \u0441\u0430\u043c\u0430\u044f \u0443\u043b\u043e\u0432\u043a\u0430, \u043f\u0440\u043e \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0448\u0435 &#8230; \u0432\u043f\u0440\u043e\u0447\u0435\u043c \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0445\u0438\u0442\u0440\u0430\u044f. \u041c\u044b \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0441\u043b\u0438\u044f\u043d\u0438\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 \u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0442\u0430\u0442\u0443\u0441 \u0432 &#171;\u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c&#187;.<\/p>\n<pre><code class=\"cpp\">inline void StopLoop()  {     if (MainThread.joinable())         MainThread.join();     TFStatus = ThreadedFuncStatus_T::no_process; } <\/code><\/pre>\n<p>\u041d\u043e \u0445\u0438\u0442\u0440\u043e\u0441\u0442\u044c\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e, \u0447\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u0434\u043b\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>StopAsync()<\/code>.<\/p>\n<h4>StopAsync()<\/h4>\n<p>\u041d\u0435 \u043e\u0442\u0445\u043e\u0434\u044f \u043e\u0442 \u043a\u0430\u0441\u0441\u044b \u0441\u0440\u0430\u0437\u0443, \u043d\u0430 \u043d\u0435\u0433\u043e \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0438 \u043d\u0430 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 <code>Stop()<\/code>.<\/p>\n<pre><code class=\"cpp\">inline bool StopAsync()  {     ThreadedFuncStatus_T _Expected = ThreadedFuncStatus_T::is_running;     const bool ret = TFStatus.compare_exchange_strong(_Expected, is_stopping);     if (ret) {         std::thread thread{ &amp;ThreadedFunc::StopLoop, this };         thread.detach();     }     return ret; } <\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a \u0442\u043e\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044f \u0441\u0442\u0430\u0442\u0443\u0441.<br \/> \u0417\u0430\u043c\u0435\u0442\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>compare_exchange_strong()<\/code> \u0443 \u0430\u0442\u043e\u043c\u0430\u0440\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u0430\u043c\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c \u0438 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442 \u043d\u0430 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0435, \u043f\u0440\u0438 \u0432\u0441\u0451\u043c \u044d\u0442\u043e\u043c \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043d\u0430\u043c \u0431\u044b\u043b\u0430 \u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0437\u0430\u043c\u0435\u043d\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f (\u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438 &#171;\u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0438 \u043e\u0431\u043c\u0435\u043d\u044f\u0442\u044c&#187;).<\/p>\n<h4>Stop()<\/h4>\n<pre><code class=\"cpp\">inline bool Stop() {     ThreadedFuncStatus_T _Expected = ThreadedFuncStatus_T::is_running;     const bool ret = TFStatus.compare_exchange_strong(_Expected, is_stopping);     if (ret) StopLoop();     return ret; } <\/code><\/pre>\n<p>\u0412\u0441\u0451 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u043d\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.<\/p>\n<h4>Run()<\/h4>\n<p>\u0417\u0430\u0431\u0430\u0432\u043d\u043e, \u0447\u0442\u043e \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u043c\u044b \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c, \u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0435\u0442, \u0442\u0430\u043a \u0447\u0442\u043e &#8230;<\/p>\n<pre><code class=\"cpp\">inline bool Run() {     assert(StoredFunction != nullptr &amp;&amp; \"No pointer to the function is presented or this object was moved\");      ThreadedFuncStatus_T _Expected = ThreadedFuncStatus_T::no_process;     const bool ret = TFStatus.compare_exchange_strong(_Expected, is_starting);     if (ret)         MainThread = std::thread{ &amp;ThreadedFunc::MainLoop, this };     return ret; } <\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043d\u0435 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u043b\u0438 \u043b\u0438 \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u0445\u0440\u0430\u043d\u0438\u043c \u043b\u0438 \u043c\u044b \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0430 \u0437\u0430\u0442\u0435\u043c \u0441\u0442\u0430\u0432\u0438\u043c \u0441\u0442\u0430\u0442\u0443\u0441 \u0438, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a.<\/p>\n<h4>Status()<\/h4>\n<pre><code class=\"cpp\">inline bool Status() const { return TFStatus != ThreadedFuncStatus_T::no_process; } <\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u044b \u043b\u043e\u0433\u0438\u0447\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0441\u0447\u0438\u0442\u0430\u0435\u043c, \u0447\u0442\u043e \u043b\u044e\u0431\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u043f\u0440\u0438\u0437\u043d\u0430\u0451\u0442\u0441\u044f \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c \u0441\u0442\u0430\u0442\u0443\u0441\u043e\u043c \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u043e\u043b\u043d\u043e\u0439 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0441\u0442\u0430\u0442\u0443\u0441 \u0432\u0435\u0440\u043d\u0451\u0442 <code>no_process<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c <code>false<\/code>.<\/p>\n<h4>WaitUntilStop()<\/h4>\n<p>\u041d\u0443 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 &#8212; \u044d\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">inline void WaitUntilStop() const  {     while (this-&gt;Status())         std::this_thread::sleep_for(std::chrono::nanoseconds(1)); } <\/code><\/pre>\n<h4>\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b<\/h4>\n<pre><code class=\"cpp\">ThreadedFunc(function_type function, function_owner_type*     owner = nullptr, handler_type handler_pred = nullptr) :     StoredFunction (function),     StoredObjectPtr(owner),     StoredHandler  (handler_pred) {     if constexpr (!std::is_same_v&lt;function_owner_type, std::nullptr_t&gt;)         assert(owner != nullptr); } <\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0443 \u043d\u0430\u0441 \u0432\u0441\u0451 \u043f\u0440\u043e\u0441\u0442\u043e, \u043d\u043e \u043e\u043f\u044f\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c <code>assert<\/code> \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0442\u043b\u0430\u0432\u043b\u0438\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u0438 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0442\u043b\u0430\u0434\u043a\u0438.<\/p>\n<pre><code class=\"cpp\">ThreadedFunc(ThreadedFunc&amp;&amp; other,   function_owner_type* new_owner = nullptr) : StoredFunction (other.StoredFunction), StoredObjectPtr(new_owner), StoredHandler  (other.StoredHandler)  {     if constexpr (!std::is_same_v&lt;function_owner_type, std::nullptr_t&gt;)         assert(new_owner != nullptr);  if (other.Status()) { other.Stop(); this-&gt;Run(); } other.StoredFunction = nullptr; } <\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u0431\u044b\u043b\u0430 \u0430\u043a\u0442\u0438\u0432\u043d\u0430 \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f, \u043c\u044b \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0446\u0438\u043a\u043b \u0432\u044b\u0437\u043e\u0432\u0430, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u043c \u0437\u0430\u043c\u0435\u043d\u0443 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430 \u0438 \u0441\u043d\u043e\u0432\u0430 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0446\u0438\u043a\u043b.<\/p>\n<h4>\u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/h4>\n<pre><code class=\"cpp\">~ThreadedFunc() { if (!Stop()) WaitUntilStop(); } <\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0439 \u0444\u0438\u043d\u0442 \u0443\u0448\u0430\u043c\u0438 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439, \u0435\u0441\u043b\u0438 <code>Stop()<\/code> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430, \u0430 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 &#8212; \u0438\u0437 \u0434\u0440\u0443\u0433\u043e\u0433\u043e.<\/p>\n<h2>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/h2>\n<pre><code class=\"cpp\">void Func1() {} bool Func2() {} ThreadedFunc&lt;void(*)(), nullptr_t&gt; TF1(Func1); ThreadedFunc&lt;bool(*)(), nullptr_t&gt; TF2(Func2);  \/\/ \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u043d\u0435\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 class Obj { public:     \/\/ \u0414\u0430, \u043c\u044b \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b \u043e\u0431\u0451\u0440\u0442\u043a\u0438,     \/\/ \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e     Obj() :          TF3(&amp;Obj::Func1, this),         TF4(&amp;Obj::Func2, this, &amp;Obj::Hander) \/\/ \u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c     {}      \/\/ \u041f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\/\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 bool \u043e\u0442\u0432\u0435\u0442     bool Hander(int res) { return bool(res); }      \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043d\u043e \u0443\u0436\u0435 \u043a\u0430\u043a \u043c\u0435\u0442\u043e\u0434\u044b     void Func1() {}     int  Func2() {}  private:     \/\/ \u0412\u043e\u0442 \u0442\u0430\u043a \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043d\u0430\u0448\u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0438     ThreadedFunc&lt;void(Obj::*)(), Obj&gt;                    TF3;     ThreadedFunc&lt;int (Obj::*)(), Obj, bool(Obj::*)(int)&gt; TF4; \/\/ \u0421 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u043c }Obj1; \/\/ \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0440\u0430\u0437\u0443 \u043e\u0434\u0438\u043d \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u0434\u0430\u043b\u0435\u0435  \/\/ \u0410 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u0451\u0440\u0442\u043e\u043a \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439-\u043c\u0435\u0442\u043e\u0434\u043e\u0432 ThreadedFunc&lt;void(Obj::*)(), Obj&gt;                    TF5(&amp;Obj::Func1, &amp;Obj1); ThreadedFunc&lt;int (Obj::*)(), Obj, bool(Obj::*)(int)&gt; TF6(&amp;Obj::Func2, &amp;Obj1); <\/code><\/pre>\n<p>\u041d\u0443 \u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <code>Run()<\/code>, <code>Stop()<\/code>, <code>StopAsync()<\/code> \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435 \u0438 &#8230; \u0440\u0430\u0434\u0443\u0435\u043c\u0441\u044f \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430!<\/p>\n<h2>\u0415\u0449\u0451 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f<\/h2>\n<p>\u041a\u0430\u043a \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u043f\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c \u043a\u043e\u0434\u0430 \u0432\u044b\u0448\u0435, \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u0438 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0437\u0435\u043d\u0430 \u0432 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0438 \u043d\u0430\u0448\u0435\u0439 \u043e\u0431\u0451\u0440\u0442\u043a\u0438, \u0432\u0441\u0451 \u0435\u0449\u0451 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u0442\u0440\u0430\u0448\u043d\u043e. \u0422\u0430\u043a \u0447\u0442\u043e \u044f \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b \u043c\u043e\u0435\u043c\u0443 \u043a\u043e\u043b\u043b\u0435\u0433\u0435 \u043f\u043e\u043b\u043e\u043c\u0430\u0442\u044c \u043c\u043e\u0437\u0433. \u0418 \u043f\u043e\u0441\u043b\u0435 \u043f\u0430\u0440\u044b \u0447\u0430\u0441\u043e\u0432 \u0436\u043e\u043d\u0433\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u043d \u0438\u0437\u043e\u0431\u0440\u0451\u043b \u042d\u0422\u041e:<\/p>\n<pre><code class=\"cpp\">\/\/\u0414\u043b\u044f \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0441\u043e\u0441\u0442\u0430\u0432\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 template&lt;typename ClassT, typename ReturnT = std::nullptr_t&gt; using ClassThreadedFunc = ThreadedFunc&lt;     std::conditional_t&lt;std::is_same_v&lt;ReturnT, std::nullptr_t&gt;, void(ClassT::*)(), ReturnT(ClassT::*)()&gt;,     ClassT,     std::conditional_t&lt;std::is_same_v&lt;ReturnT, std::nullptr_t&gt;, std::nullptr_t, bool(ClassT::*)(ReturnT)&gt;&gt;;  \/\/ \u0414\u043b\u044f \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 template&lt;typename ReturnT = std::nullptr_t&gt; using FuncThreadedFunc = ThreadedFunc&lt;     std::conditional_t&lt;std::is_same_v&lt;ReturnT, std::nullptr_t&gt;, void(*)(), ReturnT(*)()&gt;,     std::nullptr_t,     std::conditional_t&lt;std::is_same_v&lt;ReturnT, std::nullptr_t&gt;, std::nullptr_t, bool(*)(ReturnT)&gt;&gt;;  <\/code><\/pre>\n<p>\u0412\u0435\u0441\u044c \u044d\u0442\u043e\u0442 \u0443\u0436\u0430\u0441 \u0441\u0432\u043e\u0434\u0438\u0442 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043a \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u043c \u0441\u0442\u0440\u043e\u0447\u043a\u0430\u043c \u043f\u043e \u0442\u0438\u043f\u0443:<\/p>\n<pre><code class=\"cpp\">void Func1() {} bool Func2() {} FuncThreadedFunc&lt;    &gt; TF1(Func1); FuncThreadedFunc&lt;bool&gt; TF2(Func2);  \/\/ \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u043d\u0435\u043a\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 class Obj { public:     \/\/ \u0414\u0430, \u043c\u044b \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u0441\u0442\u0440\u043e\u0433\u043e \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b \u043e\u0431\u0451\u0440\u0442\u043a\u0438,     \/\/ \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e     Obj() :          TF3(&amp;Obj::Func1, this)     {}      \/\/ \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043d\u043e \u0443\u0436\u0435 \u043a\u0430\u043a \u043c\u0435\u0442\u043e\u0434\u044b     void Func1() {}  private:     \/\/ \u0412\u043e\u0442 \u0442\u0430\u043a \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043d\u0430\u0448\u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0438     ClassThreadedFunc&lt;Obj&gt; TF3; }Obj1; \/\/ \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0440\u0430\u0437\u0443 \u043e\u0434\u0438\u043d \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u0434\u0430\u043b\u0435\u0435 \/\/ \u0410 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u0451\u0440\u0442\u043e\u043a \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439-\u043c\u0435\u0442\u043e\u0434\u043e\u0432 ClassThreadedFunc&lt;Obj&gt; TF4(&amp;Obj::Func1, &amp;Obj1); <\/code><\/pre>\n<h2>\u041f\u043e\u0441\u043b\u0435\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>\u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u044b \u0438 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0441\u0443\u043f\u0435\u0440 \u043e\u0442\u043b\u0430\u0436\u0435\u043d\u043d\u044b\u0439 \u0438 \u0434\u0437\u0435\u043d\u043e\u0432\u0441\u043a\u0438\u0439 \u043a\u043e\u0434, \u043d\u043e \u0432\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u043b\u0430 \u043d\u0430\u0441 \u043d\u0430\u043f\u0440\u044f\u0447\u044c \u043c\u043e\u0437\u0433\u0438 \u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 <code>type_traits<\/code>.<br \/> \u041d\u0430 \u043d\u0430\u0448 \u0441\u043a\u0440\u043e\u043c\u043d\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043d\u0435\u043f\u043b\u043e\u0445\u043e\u0439 \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0418\u0437 \u043d\u044e\u0430\u043d\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043d\u0440\u0430\u0432\u044f\u0442\u0441\u044f \u043c\u043d\u0435 \u0441\u0430\u043c\u043e\u043c\u0443 &#8212; \u044d\u0442\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0442\u0440\u0435\u0442\u044c\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 <code>Stop()<\/code> \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0442\u043e\u0434 <code>StopAsync()<\/code>, \u043e\u0434\u043d\u0430\u043a\u043e \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u044c\u0438 \u043c\u044b \u043d\u0435 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b\u0438, \u043a\u0430\u043a \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u044d\u0442\u043e\u0433\u043e.<\/p>\n<p>\u041d\u0430\u0434\u0435\u044e\u0441\u044c \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0443\u0434\u0438\u0432\u0438\u043b\u0430 \u0412\u0430\u0441 \u0438\u043b\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u0434\u0430\u043b\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u0443\u044e \u0436\u0432\u0430\u0447\u043a\u0443 \u0434\u043b\u044f \u043c\u043e\u0437\u0433\u0430, \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/938382\/\"> https:\/\/habr.com\/ru\/articles\/938382\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435<\/h2>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430\u0447\u043d\u0451\u043c \u043c\u044b \u0441 \u043f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0437\u0430\u0434\u0430\u0447\u0438: \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043d\u0435\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0438\u043c\u0435\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u0434\u043b\u044f \u0435\u0451 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \/ \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u0442\u0430\u0442\u0443\u0441\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \/ \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438 \u043f\u0440\u043e\u0447\u0435\u0435. \u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043a\u043b\u0430\u0441\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434:<\/p>\n<pre><code class=\"cpp\">using namespace std;  class Object \/\/ \u043d\u0430\u0448 \u043d\u0435\u043a\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446 \u043c\u0435\u0442\u043e\u0434\u0430 { public:     Object() : \/\/ \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043d\u0438\u0447\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e         FuncThread(nullptr),         FuncStatus(false)     {}     ~Object() \/\/ \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440     {         StopFunc(); \/\/ \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u0438     }     void Func() \/\/ \u0441\u0430\u043c\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a     {         while (FuncStatus)         {             \/\/Do something         }     }     bool StartFunc() \/\/ \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0435\u0451 \u0437\u0430\u043f\u0443\u0441\u043a\u0430     {         if (FuncThread || FuncStatus)             return false;         FuncThread = new thread(&amp;Object::Func, this);         FuncStatus = true;         return true;     }     bool StopFunc() \/\/ \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0435\u0451 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438     {         if (!FuncThread &amp;&amp; !FuncStatus)             return false;         FuncStatus = false;         if (FuncThread &amp;&amp; FuncThread-&gt;joinable()) FuncThread-&gt;join();         if (FuncThread) delete FuncThread;         FuncThread = nullptr;         return true;     }     bool Status() { return FuncStatus; } \/\/ \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0435\u0451 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f  private:     thread* FuncThread; \/\/ \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u043e\u0442\u043e\u043a, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u043a\u0440\u0443\u0442\u0438\u0442\u044c\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f     mutex FuncMutex; \/\/ \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043b\u044f \u043e\u0431\u043c\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0441 \u043f\u043e\u0442\u043e\u043a\u043e\u043c     atomic&lt;bool&gt; FuncStatus; \/\/ \u0441\u0442\u0430\u0442\u0443\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043e\u043d \u0436\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0444\u043b\u0430\u0433 }; <\/code><\/pre>\n<p>\u041d\u0430\u0434\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e <code>mutex<\/code> \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043d\u0430\u0434\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438, \u0434\u043b\u044f \u043e\u0431\u043c\u0435\u043d\u0430 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c \u0438 \u043f\u043e\u0442\u043e\u043a\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0430 <code>FuncStatus<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u044b\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c \u0442\u0438\u043f\u043e\u043c \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u0443\u0441\u0430 \u0440\u0430\u0431\u043e\u0442\u044b.<br \/> \u041d\u0443 \u0430 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0435-\u0442\u0440\u0438 \u0442\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435. \u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430? \u0427\u0435\u0441\u0442\u043d\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0437\u0432\u0443\u0447\u0438\u0442 \u043a\u0430\u043a \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0433\u043e\u043b\u043e\u0432\u043d\u0430\u044f \u0431\u043e\u043b\u044c.<br \/> \u041c\u043d\u043e\u044e \u0438 \u043c\u043e\u0438\u043c \u043a\u043e\u043b\u043b\u0435\u0433\u043e\u0439 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u0435\u043a\u0443\u044e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u0443\u044e \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0434\u043b\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0438 \u043c\u044b \u043f\u0435\u0440\u0435\u0448\u043b\u0438 \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f:<\/p>\n<ol>\n<li>\n<p>\u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u044f\u043c\u043e \u0438\u0437 \u043d\u0435\u0451 \u0436\u0435<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u0432\u043e\u0434 \u043e \u0435\u0451 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0432 \u0441\u0442\u0430\u0442\u0443\u0441<\/p>\n<\/li>\n<li>\n<p>\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<\/li>\n<\/ol>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u0441\u0440\u0430\u0437\u0443 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u0438 \u0434\u043b\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432.<\/p>\n<h2>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 <code>type_traits<\/code>, \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0448 \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043f\u043e\u043c\u043e\u0449\u043d\u0438\u043a \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0438, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043d\u0451\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u043c\u043e\u0449\u043d\u0435\u0439\u0448\u0438\u0435 \u0438, \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f, \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043e\u0431\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u0441\u0442\u0432 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0439 \u043d\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u043f\u0440\u043e\u0447\u0435\u0433\u043e.<\/p>\n<pre><code class=\"cpp\">\/\/ Function template&lt;typename Callable&gt; struct callable_decay {     using return_type = std::invoke_result_t&lt;Callable&gt;;     using owner_type  = void; }; \/\/ Function with args template&lt;typename Return, typename... Args&gt; struct callable_decay&lt;Return(Args...)&gt; {     using return_type = Return;     using owner_type  = void; }; \/\/ Member pointer function template&lt;typename Return, typename Class, typename... Args&gt; struct callable_decay&lt;Return(Class ::*)(Args...)&gt;  {     using return_type = Return;     using owner_type  = Class; }; \/\/ Const member pointer function template&lt;typename Return, typename Class, typename... Args &gt; struct callable_decay&lt;Return(Class::*)(Args...) const&gt;  {     using return_type = Return;     using owner_type  = Class; };  template&lt;typename Callable&gt; using callable_return_type_t = typename callable_decay&lt;Callable&gt;::return_type; template&lt;typename Callable&gt; using callable_owner_type_t  = typename callable_decay&lt;Callable&gt;::owner_type; <\/code><\/pre>\n<p>\u041c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>callable_decay<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0442\u0438\u043f &#171;\u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430&#187; \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441, \u0435\u0441\u043b\u0438 \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u043e\u0431\u0451\u0440\u0442\u043a\u0443 \u043c\u0435\u0442\u043e\u0434. \u0422\u0430\u043a\u0436\u0435 \u043e\u0431\u044a\u044f\u0432\u0438\u043c <code>callable_return_type_t<\/code> \u0438 <code>callable_owner_type_t <\/code> \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448 \u043a\u043e\u0434 \u043d\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0442\u0430\u043a \u0441\u0442\u0440\u0430\u0448\u043d\u043e \u0438 \u043d\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043e.<\/p>\n<pre><code class=\"cpp\">\/\/ Threaded func possible states enum ThreadedFuncStatus_T {     no_process,     is_starting,     is_running,     is_stopping }; <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0434\u043b\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<ol>\n<li>\n<p>\u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442<\/p>\n<\/li>\n<li>\n<p>\u0441\u0442\u0430\u0440\u0442\u0443\u0435\u043c<\/p>\n<\/li>\n<li>\n<p>\u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u0441\u044f<\/p>\n<\/li>\n<li>\n<p>\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c\u0441\u044f<\/p>\n<\/li>\n<\/ol>\n<h2>\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043a\u043b\u0430\u0441\u0441<\/h2>\n<h3>\u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435<\/h3>\n<pre><code class=\"cpp\">\/\/ function_type - \u0442\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u043e\u0431\u0451\u0440\u0442\u043a\u0435 \/\/ function_owner_type  - \u0442\u0438\u043f \"\u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430\" \u0444\u0443\u043d\u043a\u0446\u0438\u0438 (\u043a\u043b\u0430\u0441\u0441\u0430) \/\/ handler_type - \u0442\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 template&lt;typename function_type,           typename function_owner_type = std::nullptr_t,           typename handler_type        = std::nullptr_t&gt; class ThreadedFunc { public: \/\/ \u0412\u044b\u044f\u0441\u043d\u044f\u0435\u043c \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f using function_return_type = callable_return_type_t&lt;function_type&gt;;      \/\/ \u0410 \u0432\u043e\u0442 \u0442\u0443\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u043b \u043b\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u043d\u0430\u043c \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u043e\u0442 \u0436\u0435 \u0442\u0438\u043f \"\u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430\" \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0447\u0442\u043e \u0438 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043d\u0430\u043c \u0441\u0430\u043c\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f     static_assert(!std::is_member_function_pointer_v&lt;function_type&gt; ||          std::is_same_v&lt;callable_owner_type_t&lt;function_type&gt;, function_owner_type&gt;,         \"Invalid function owner type\");      \/\/ \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440     ThreadedFunc(function_type function, function_owner_type*     owner = nullptr, handler_type handler_pred = nullptr);     \/\/ \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u043d\u043e\u0432\u043e\u0433\u043e \u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0433\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u044f     ThreadedFunc(ThreadedFunc&amp;&amp; other,   function_owner_type* new_owner = nullptr);     \/\/ \u0410 \u0432\u043e\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u044b \u0443\u0434\u0430\u043b\u044f\u0435\u043c     ThreadedFunc(const ThreadedFunc&amp;) = delete;     \/\/ \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u043d\u0438\u0447\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e     ~ThreadedFunc();      \/\/ \u0422\u0430\u043a\u0436\u0435 \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u044f     ThreadedFunc&amp; operator=(const ThreadedFunc&amp;) = delete;      \/\/ \u041c\u0435\u0442\u043e\u0434\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430, \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u0442\u0443\u0441\u0430     inline bool Run();     inline bool Stop();     inline bool StopAsync();     inline void WaitUntilStop() const;     inline bool Status() const;  private:     \/\/ \u0425\u0438\u0442\u0440\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438     inline void StopLoop();     \/\/ \u041c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \"\u0445\u0440\u0430\u043d\u0438\u043c\u043e\u0439\" \u0444\u0443\u043d\u043a\u0446\u0438\u0438     inline function_return_type CallStoredFunction();     \/\/ \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0446\u0438\u043a\u043b \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u0430     void MainLoop();      function_type  StoredFunction;  \/\/ \u0425\u0440\u0430\u043d\u0438\u043c\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f     function_owner_type* const  StoredObjectPtr; \/\/ \u0425\u0440\u0430\u043d\u0438\u043c\u0430\u044f \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \"\u0432\u043b\u0430\u0434\u0435\u043b\u044c\u0446\u0430\"     handler_type  StoredHandler;   \/\/ \u0425\u0440\u0430\u043d\u0438\u043c\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a      std::atomic&lt;ThreadedFuncStatus_T&gt; TFStatus; \/\/ \u041d\u0430\u0448 \u0441\u0442\u0430\u0442\u0443\u0441     std::thread  MainThread; \/\/ \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0441\u0430\u043c \u043f\u043e\u0442\u043e\u043a }; <\/code><\/pre>\n<p>\u041f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432, \u0432\u0441\u0451 \u0442\u0430\u043a\u0438 \u043f\u043e\u044f\u0441\u043d\u044e \u043f\u0430\u0440\u0443 \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0445 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432.<br \/> \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, <code>static_assert<\/code> \u043a\u0440\u0430\u0439\u043d\u0435 \u0432\u0430\u0436\u0435\u043d, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0435\u0434\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u043d\u0430\u0441 \u0441\u0430\u043c\u0438\u0445 \u0436\u0435 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u043e\u0442 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u0432 \u043e\u0431\u0451\u0440\u0442\u043a\u0443, \u0447\u0442\u043e\u0431\u044b \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043d\u0435 \u0438\u0441\u043a\u0430\u0442\u044c \u0433\u0434\u0435 \u0438 \u043a\u0430\u043a \u043c\u044b \u043e\u043f\u043b\u043e\u0448\u0430\u043b\u0438.<br \/> \u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u043e\u0431\u0440\u0430\u0449\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u044b <code>StopAsync()<\/code> \u0438 <code>WaitUntilStop()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u043e\u0431\u0451\u0440\u0442\u043a\u0438, \u0434\u0430\u0436\u0435 \u0438\u0437 \u0441\u0430\u043c\u043e\u0439 \u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u0451\u0440\u0442\u043a\u0438! \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u043e\u0442\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0442\u043e\u0447\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438.<br \/> \u0412-\u0442\u0440\u0435\u0442\u044c\u0438\u0445, \u0441\u0442\u043e\u0438\u0442 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e \u043d\u0435\u043a\u0438\u0439 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043e \u043d\u0451\u043c \u043c\u044b \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043d\u0438\u0436\u0435, \u0430 \u043f\u043e\u043a\u0430 \u0441\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0433\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438 \u043c\u044b \u043d\u0435 \u0437\u0430\u0432\u044f\u0437\u0430\u043d\u044b \u043d\u0430 \u0435\u0433\u043e \u0442\u0438\u043f.<br \/> \u041d\u0443 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435, \u0447\u0442\u043e \u044f \u0437\u0430\u043c\u0435\u0447\u0443 &#8212; \u044d\u0442\u043e \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043f\u0440\u0438\u0441\u0432\u043e\u0435\u043d\u0438\u044f, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u0442\u0430\u043a \u0438 \u043d\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u0438\u0439\u0442\u0438 \u043a \u0435\u0434\u0438\u043d\u043e\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e \u043f\u043e \u043f\u043e\u0432\u043e\u0434\u0443 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043e\u0431\u0451\u0440\u0442\u043a\u0438 \u0432 \u044d\u0442\u0438\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445, \u043e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u043e \u043d\u0435 \u043c\u0435\u0448\u0430\u0435\u0442 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435.<\/p>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/h3>\n<h4>CallStoredFunction()<\/h4>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u0437\u0430\u0433\u0430\u0434\u043e\u0447\u043d\u043e\u0433\u043e \u0438 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e &#8212; \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u0445\u0440\u0430\u043d\u0438\u043c\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<pre><code class=\"cpp\">inline function_return_type CallStoredFunction()  {     if constexpr (std::is_member_function_pointer_v&lt;function_type&gt;)         return (StoredObjectPtr-&gt;*StoredFunction)();     else         return (*StoredFunction)(); } <\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u043d\u0438\u0447\u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u043d\u0435\u0442, \u043e\u0434\u043d\u0430\u043a\u043e <code>constexpr<\/code> \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0438 \u0440\u0430\u0437\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u043c\u0430\u0448\u0438\u043d\u0443 \u043e\u0442 \u043b\u0438\u0448\u043d\u0438\u0445 \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u0439 \u043e \u0442\u0438\u043f\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0435\u0449\u0451 \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0432\u0435\u0434\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f-\u0447\u043b\u0435\u043d \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u043d\u0435\u0437\u0430\u043f\u043d\u043e \u0441\u0442\u0430\u0442\u044c \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0439 \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442.<\/p>\n<h4>MainLoop()<\/h4>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0443\u0436\u0435 \u0443\u043c\u0435\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0443\u0436\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c, \u043f\u043e\u0440\u0430 \u0431\u044b \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430<\/p>\n<pre><code class=\"cpp\">void MainLoop() {     TFStatus = ThreadedFuncStatus_T::is_running;     bool handler_error = false;      while (true)     {         if (TFStatus == ThreadedFuncStatus_T::is_stopping || handler_error)             break;          if constexpr (std::is_same_v&lt;function_return_type, void&gt;)             \/\/ no return type             CallStoredFunction();         else if constexpr (std::is_same_v&lt;handler_type, bool(*)(function_return_type)&gt;)             \/\/ a handler is presented             handler_error = (*StoredHandler)(CallStoredFunction());         else             \/\/ no handler - treating return type as bool             handler_error = static_cast&lt;bool&gt;(CallStoredFunction());     } } <\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0441 \u0442\u0435\u043c \u0436\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0441\u043b\u043e\u0432\u043e\u043c <code>constexpr<\/code> \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u0442\u0440\u0438 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u0451\u0440\u0442\u043a\u0438:<\/p>\n<ol>\n<li>\n<p>\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0438 \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0451 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c<\/p>\n<\/li>\n<li>\n<p>\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a (\u043f\u0440\u0438 \u0435\u0433\u043e \u043d\u0430\u043b\u0438\u0447\u0438\u0438) \u0438 \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0432\u043e\u0434 \u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u044f \u0443\u0436\u0435 \u0438\u0437 \u043e\u0442\u0432\u0435\u0442\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430<\/p>\n<\/li>\n<li>\n<p>\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0430\u043c \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043d\u0430\u043c, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043b\u0438 \u0440\u0430\u0431\u043e\u0442\u0443 \u0431\u0435\u0437 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u0440\u0438 \u0432\u0441\u0451\u043c \u044d\u0442\u043e\u043c \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0444\u043b\u0430\u0433 \u0441\u0442\u0430\u0442\u0443\u0441\u0430 \u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043b\u0438 \u0434\u0430\u043b\u044c\u0448\u0435 \u043a\u0440\u0443\u0442\u0438\u0442\u044c\u0441\u044f \u0438\u043b\u0438 \u043f\u043e\u0440\u0430 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u0438\u0437-\u0437\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0438\u0437\u0432\u043d\u0435.<\/p>\n<h4>\u041f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e\u0431 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435<\/h4>\n<p>\u0421\u0442\u043e\u0438\u0442 \u0432\u0441\u0451 \u0442\u0430\u043a\u0438 \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0441\u043b\u043e\u0432 \u043e\u0431 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0435. \u0414\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043c\u044b \u0441 \u043a\u043e\u043b\u043b\u0435\u0433\u043e\u0439 \u0442\u0430\u043a \u0438 \u043d\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u043f\u0440\u0438\u0439\u0442\u0438 \u043a \u0435\u0434\u0438\u043d\u043e\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e \u043e\u0431 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u044d\u0442\u043e\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u0430, \u043d\u043e \u0432\u0441\u0451 \u0436\u0435 \u0440\u0435\u0448\u0438\u043b\u0438, \u0447\u0442\u043e \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u043d\u0435\u043a\u043e\u0433\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 \u0432\u0438\u0434\u0435 \u043e\u0448\u0438\u0431\u043e\u043a (\u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 <code>boost::system::error_code<\/code> \u0438 \u0438\u043c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u043c), \u0431\u044b\u043b\u043e \u0431\u044b \u0437\u0434\u043e\u0440\u043e\u0432\u043e \u0438\u043c\u0435\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\/\u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a \u0442\u0430\u043a\u0438\u0445 \u0432\u043e\u0442 \u043e\u0448\u0438\u0431\u043e\u043a \u043a \u043f\u0440\u043e\u0441\u0442\u043e\u043c\u0443 \u0432\u044b\u0432\u043e\u0434\u0443: \u0441\u0442\u043e\u0438\u0442 \u043b\u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0438\u043b\u0438 \u043c\u044b \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0438\u0441\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e\u0439 \u043e\u0448\u0438\u0431\u043a\u043e\u0439.<br \/> \u0414\u0430, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u044d\u0442\u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u043c\u043e\u0436\u0435\u0442<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-471176","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/471176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=471176"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/471176\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=471176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=471176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=471176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}