{"id":320032,"date":"2021-03-22T15:00:23","date_gmt":"2021-03-22T15:00:23","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=320032"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=320032","title":{"rendered":"\u041d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0432 C++20: std::jthread"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<blockquote>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/Yf6l\/\">&#171;<strong>C++ Developer. Professional<\/strong>&#171;<\/a><\/p>\n<\/blockquote>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fb8\/510\/0df\/fb85100dfeb5dc106dca8dcc11cc3615.png\" width=\"780\" height=\"439\"><figcaption><\/figcaption><\/figure>\n<hr>\n<p>\u041e\u0434\u0438\u043d \u0438\u0437 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u043c\u043e\u0435\u0433\u043e <a href=\"https:\/\/cppcon.org\/cppcon-2018-program\/\">\u0441\u0435\u043c\u0438\u043d\u0430\u0440\u0430 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 CppCon 2018<\/a> \u0441\u043f\u0440\u043e\u0441\u0438\u043b \u043c\u0435\u043d\u044f: \u00ab\u041c\u043e\u0436\u0435\u0442 \u043b\u0438 <code>std::thread<\/code> \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0440\u0432\u0430\u043d (interrupted)?\u00bb. \u041c\u043e\u0439 \u043e\u0442\u0432\u0435\u0442 \u0442\u043e\u0433\u0434\u0430 \u0431\u044b\u043b \u2014 \u043d\u0435\u0442, \u043d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0442\u0430\u043a. \u0421 C++20 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <code>std::jthread<\/code> (\u0432 \u0438\u0442\u043e\u0433\u0435 \u0432\u0441\u0435 \u0442\u0430\u043a\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u2014 \u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430).<\/p>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u0440\u0430\u0437\u0432\u0438\u0442\u044c \u0442\u0435\u043c\u0443, \u043f\u043e\u0434\u043d\u044f\u0442\u0443\u044e \u043d\u0430 CppCon 2018. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0435\u0440\u0435\u0440\u044b\u0432\u0430 \u0432 \u043c\u043e\u0435\u043c \u0441\u0435\u043c\u0438\u043d\u0430\u0440\u0435, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0443, \u044f \u043f\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043b \u0441 \u041d\u0438\u043a\u043e\u043b\u0430\u0435\u043c (\u0419\u043e\u0441\u0443\u0442\u0442\u0438\u0441\u043e\u043c). \u041e\u043d \u0441\u043f\u0440\u043e\u0441\u0438\u043b \u043c\u0435\u043d\u044f, \u0447\u0442\u043e \u044f \u0434\u0443\u043c\u0430\u044e \u043e \u043d\u043e\u0432\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2018\/p0660r5.pdf\">P0660: Cooperatively Interruptible Joining Thread<\/a>. \u041d\u0430 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u044f \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u043b \u043e\u0431 \u044d\u0442\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u041d\u0438\u043a\u043e\u043b\u0430\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f (\u043d\u0430\u0440\u044f\u0434\u0443 \u0441 \u0425\u0435\u0440\u0431\u043e\u043c \u0421\u0430\u0442\u0442\u0435\u0440\u043e\u043c \u0438 \u042d\u043d\u0442\u043e\u043d\u0438 \u0423\u0438\u043b\u044c\u044f\u043c\u0441\u043e\u043c). \u0421\u0435\u0433\u043e\u0434\u043d\u044f\u0448\u043d\u044f\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u043c\u0443 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0432 C++. \u041d\u0438\u0436\u0435 \u044f \u043f\u0440\u0438\u0432\u0435\u043b \u043e\u0431\u0449\u0443\u044e \u043a\u0430\u0440\u0442\u0438\u043d\u0443 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0438 \u0433\u0440\u044f\u0434\u0443\u0449\u0435\u043c C++.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/749\/bb3\/018\/749bb30183a41c53cb9d5d5a1addf676.png\" width=\"869\" height=\"389\"><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2018\/p0660r5.pdf\">Cooperatively Interruptible Joining Thread<\/a> (\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c\u044b\u0439 \u043f\u043e\u0442\u043e\u043a) \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438\u043c\u0435\u0435\u0442 \u0434\u0432\u0435 \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438: \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u043c\u043e\u0441\u0442\u044c (interruptible) \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 (automatically joining, \u0437\u0434\u0435\u0441\u044c \u0438 \u0434\u0430\u043b\u0435\u0435 \u00ab\u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u00bb \u2014 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0434\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 <code>join()<\/code> \u2014 \u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430). \u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u0430\u043c \u043e\u0431 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438.<\/p>\n<h3>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435<\/h3>\n<p>\u042d\u0442\u043e \u043d\u0435\u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 <code>std::thread<\/code>. \u0415\u0441\u043b\u0438 <code>std::thread<\/code> \u0432\u0441\u0435 \u0435\u0449\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>joinable<\/code>, \u0442\u043e \u0432 \u0435\u0433\u043e \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>std::terminate<\/code>. \u041f\u043e\u0442\u043e\u043a <code>thr<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>joinable<\/code>, \u0435\u0441\u043b\u0438 \u043d\u0438 <code>thr.join()<\/code>, \u043d\u0438 <code>thr.detach()<\/code> \u0435\u0449\u0435 \u043d\u0435 \u0431\u044b\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043d\u044b.<\/p>\n<pre><code class=\"cpp\">\/\/ threadJoinable.cpp  #include &lt;iostream&gt; #include &lt;thread&gt;  int main(){          std::cout &lt;&lt; std::endl;     std::cout &lt;&lt; std::boolalpha;          std::thread thr{[]{ std::cout &lt;&lt; \"Joinable std::thread\" &lt;&lt; std::endl; }};          std::cout &lt;&lt; \"thr.joinable(): \" &lt;&lt; thr.joinable() &lt;&lt; std::endl;          std::cout &lt;&lt; std::endl;      }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/486\/c82\/8f4\/486c828f480b3f818760f3d39c5a0fda.png\" width=\"644\" height=\"359\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0430 \u043f\u043e\u0442\u043e\u043a\u0430 \u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u0443\u044e\u0442\u0441\u044f. \u041d\u0430 \u0432\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u043e\u0442\u043e\u043a <code>th<\/code> \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0441\u0432\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435: <code>\u00abJoinable std::thread\u00bb<\/code>.<\/p>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044f \u0437\u0430\u043c\u0435\u043d\u044f\u044e \u0445\u0435\u0434\u0435\u0440 <code>&lt;thread&gt;<\/code> \u043d\u0430 &#171;<code>jthread.hpp<\/code>&#187; \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>std::jthread<\/code> \u0438\u0437 \u0433\u0440\u044f\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430 C++.<\/p>\n<pre><code class=\"cpp\">\/\/ jthreadJoinable.cpp  #include &lt;iostream&gt; #include \"jthread.hpp\"  int main(){          std::cout &lt;&lt; std::endl;     std::cout &lt;&lt; std::boolalpha;          std::jthread thr{[]{ std::cout &lt;&lt; \"Joinable std::thread\" &lt;&lt; std::endl; }};          std::cout &lt;&lt; \"thr.joinable(): \" &lt;&lt; thr.joinable() &lt;&lt; std::endl;          std::cout &lt;&lt; std::endl;      }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0442\u043e\u043a <code>thr<\/code> \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0441\u0432\u043e\u0435\u043c \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435, \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u0441\u0435 \u0435\u0449\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>joinable<\/code>, \u043a\u0430\u043a, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c4f\/1af\/61e\/c4f1af61e8f5fd3c410b359cdd9ba1c9.png\" width=\"443\" height=\"224\"><figcaption><\/figcaption><\/figure>\n<h3>\u041f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435 std::jthread<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u043e\u0442 \u0447\u0435\u0433\u043e \u043e\u0442\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0442\u044c\u0441\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440.<\/p>\n<pre><code class=\"cpp\">\/\/ interruptJthread.cpp  #include \"jthread.hpp\" #include &lt;chrono&gt; #include &lt;iostream&gt;  using namespace::std::literals;  int main(){          std::cout &lt;&lt; std::endl;          std::jthread nonInterruptable([]{                                   \/\/ (1)         int counter{0};         while (counter &lt; 10){             std::this_thread::sleep_for(0.2s);             std::cerr &lt;&lt; \"nonInterruptable: \" &lt;&lt; counter &lt;&lt; std::endl;              ++counter;         }     });          std::jthread interruptable([](std::interrupt_token itoken){         \/\/ (2)         int counter{0};         while (counter &lt; 10){             std::this_thread::sleep_for(0.2s);             if (itoken.is_interrupted()) return;                        \/\/ (3)             std::cerr &lt;&lt; \"interruptable: \" &lt;&lt; counter &lt;&lt; std::endl;              ++counter;         }     });          std::this_thread::sleep_for(1s);          std::cerr &lt;&lt; std::endl;     std::cerr &lt;&lt; \"Main thread interrupts both jthreads\" &lt;&lt; std:: endl;     nonInterruptable.interrupt();     interruptable.interrupt();                                          \/\/ (4)          std::cout &lt;&lt; std::endl;      }<\/code><\/pre>\n<p>\u042f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u0432 main \u0434\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430, <code>nonInterruptable<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0442\u044c, \u0438 <code>interruptable<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e (\u0441\u0442\u0440\u043e\u043a\u0438 1 \u0438 2). \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u043f\u043e\u0442\u043e\u043a\u0430 <code>nonInterruptable<\/code>, \u043f\u043e\u0442\u043e\u043a <code>interruptable<\/code>, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 <code>std::interrupt_token<\/code> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0435\u0433\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 3, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0431\u044b\u043b \u043b\u0438 \u043e\u043d \u043f\u0440\u0435\u0440\u0432\u0430\u043d: <code>itoken.is_interrupted()<\/code>. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u0432 \u043b\u044f\u043c\u0431\u0434\u0435 \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 <code>return<\/code> \u0438, \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f. \u0412\u044b\u0437\u043e\u0432 <code>interruptable.interrupt()<\/code> (\u0441\u0442\u0440\u043e\u043a\u0430 4) \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0438\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0430. \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 <code>nonInterruptable.interrupt()<\/code> \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u0430 <code>nonInterruptable<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0441\u0432\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/230\/883\/55b\/23088355bd1735b7de501de9e567dbfe.png\" width=\"735\" height=\"702\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u043e\u0442 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u043e\u043a\u0435\u043d\u0430\u0445 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f (interrupt tokens), \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u0430\u0445 \u0438 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445.<\/p>\n<h4>\u0422\u043e\u043a\u0435\u043d\u044b \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f<\/h4>\n<p>\u0422\u043e\u043a\u0435\u043d \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f <code>std::interrupt_token<\/code> \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u0435\u0442 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0435 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 (shared ownership) \u0438 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438, \u0435\u0441\u043b\u0438 \u0442\u043e\u043a\u0435\u043d \u0432\u0430\u043b\u0438\u0434\u0435\u043d. \u041e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430: <code>valid<\/code>, <code>is_interrupted<\/code>, \u0438 <code>interrupt<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/836\/b23\/dff\/836b23dff2892db4afe46b6feb406bed.png\" width=\"958\" height=\"313\"><figcaption><\/figcaption><\/figure>\n<p><code>itoken.valid() \u2014 true<\/code>, \u0435\u0441\u043b\u0438 \u0442\u043e\u043a\u0435\u043d \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0434\u043b\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438<\/p>\n<p><code>itoken.is_interrupted() \u2014 true<\/code>, \u0435\u0441\u043b\u0438 \u0431\u044b\u043b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u0441 <code>true<\/code> \u0438\u043b\u0438 \u0431\u044b\u043b \u0432\u044b\u0437\u0432\u0430\u043d \u043c\u0435\u0442\u043e\u0434\u0430 <code>interrupt()<\/code><\/p>\n<p><code>itoken.interrupt()<\/code> \u2014 \u0435\u0441\u043b\u0438 <code>!valid()<\/code> \u0438\u043b\u0438 <code>is_interrupted()<\/code>, \u0442\u043e \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0435 \u0432\u043e\u0437\u044b\u043c\u0435\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0430. \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c <code>itoken.is_interrupted() == true<\/code>. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>is_interrupted()<\/code><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0442\u043e\u043a\u0435\u043d \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u043c \u0442\u043e\u043a\u0435\u043d\u043e\u043c. \u0414\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u043d\u0435 \u0432\u0430\u043b\u0438\u0434\u0435\u043d. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442, \u043a\u0430\u043a \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u043b\u044b.<\/p>\n<pre><code class=\"cpp\">std::jthread jthr([](std::interrupt_token itoken){     ...     std::interrupt_token interruptDisabled;      std::swap(itoken, interruptDisabled);     \/\/ (1)            ...     std::swap(itoken, interruptDisabled);     \/\/ (2)     ... }<\/code><\/pre>\n<p><code>std::interrupt_token interruptDisabled<\/code> \u043d\u0435 \u0432\u0430\u043b\u0438\u0434\u0435\u043d. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 (1) \u0438 (2), \u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 (2) \u0443\u0436\u0435 \u043c\u043e\u0436\u0435\u0442.<\/p>\n<h4>\u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/h4>\n<p><code>std::jhread<\/code> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 <code>std::thread<\/code> \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u043c \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435. \u0414\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c <code>std::interrupt_token<\/code>.<\/p>\n<h4>\u041d\u043e\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 Wait \u0434\u043b\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445<\/h4>\n<p>\u0414\u0432\u0435 \u0432\u0430\u0440\u0438\u0430\u0446\u0438\u0438 <code>wait wait_for<\/code> \u0438 <code>wait_until<\/code> \u0438\u0437 <code>std::condition_variable<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0442 \u043d\u043e\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438. \u041e\u043d\u0438 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 <code>std::interrupt_token<\/code>.<\/p>\n<pre><code class=\"cpp\">template &lt;class Predicate&gt; bool wait_until(unique_lock&lt;mutex&gt;&amp; lock,                  Predicate pred,                  interrupt_token itoken);  template &lt;class Rep, class Period, class Predicate&gt; bool wait_for(unique_lock&lt;mutex&gt;&amp; lock,                const chrono::duration&lt;Rep, Period&gt;&amp; rel_time,                Predicate pred,                interrupt_token itoken);  template &lt;class Clock, class Duration, class Predicate&gt; bool wait_until(unique_lock&lt;mutex&gt;&amp; lock,                  const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time,                  Predicate pred,                  interrupt_token itoken);<\/code><\/pre>\n<p>\u041d\u043e\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442. \u042d\u0442\u0438 \u0432\u0435\u0440\u0441\u0438\u0438 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0441\u0438\u0433\u043d\u0430\u043b \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u043c <code>std::interrupt_token itoken<\/code>. \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <code>wait<\/code> \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043b\u0438 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435.<\/p>\n<pre><code class=\"cpp\">cv.wait_until(lock, predicate, itoken); if (itoken.is_interrupted()){     \/\/ interrupt occurred }<\/code><\/pre>\n<h4>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435?<\/h4>\n<p>\u041a\u0430\u043a \u044f \u0438 \u043e\u0431\u0435\u0449\u0430\u043b \u0432 \u0441\u0432\u043e\u0435\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u043c\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432 (concepts).<\/p>\n<hr>\n<blockquote>\n<p><em>\u0423\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043a\u0443\u0440\u0441\u0435 <\/em><a href=\"https:\/\/otus.pw\/Yf6l\/\"><em>&#171;<\/em><strong><em>C++ Developer. Professional<\/em><\/strong><em>&#171;<\/em><\/a><em>.<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u0435\u043c\u043e-\u0437\u0430\u043d\u044f\u0442\u0438\u044f \u043f\u043e \u0442\u0435\u043c\u0435 <\/em><a href=\"https:\/\/otus.pw\/WYur\/\"><strong><em>\u00ab\u041e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u043d\u0435\u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438\u00bb<\/em><\/strong><\/a><em>: \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u0449\u0435\u0433\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e unit-\u0442\u0435\u0441\u0442\u043e\u0432 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c googletest.<\/em><\/p>\n<\/blockquote>\n<\/div>\n<p> \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\/company\/otus\/blog\/548316\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/548316\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<blockquote>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/Yf6l\/\">&#171;<strong>C++ Developer. Professional<\/strong>&#171;<\/a><\/p>\n<\/blockquote>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<hr>\n<p>\u041e\u0434\u0438\u043d \u0438\u0437 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u043c\u043e\u0435\u0433\u043e <a href=\"https:\/\/cppcon.org\/cppcon-2018-program\/\">\u0441\u0435\u043c\u0438\u043d\u0430\u0440\u0430 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 CppCon 2018<\/a> \u0441\u043f\u0440\u043e\u0441\u0438\u043b \u043c\u0435\u043d\u044f: \u00ab\u041c\u043e\u0436\u0435\u0442 \u043b\u0438 <code>std::thread<\/code> \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u0440\u0432\u0430\u043d (interrupted)?\u00bb. \u041c\u043e\u0439 \u043e\u0442\u0432\u0435\u0442 \u0442\u043e\u0433\u0434\u0430 \u0431\u044b\u043b \u2014 \u043d\u0435\u0442, \u043d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0442\u0430\u043a. \u0421 C++20 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <code>std::jthread<\/code> (\u0432 \u0438\u0442\u043e\u0433\u0435 \u0432\u0441\u0435 \u0442\u0430\u043a\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u2014 \u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430).<\/p>\n<p>\u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u0440\u0430\u0437\u0432\u0438\u0442\u044c \u0442\u0435\u043c\u0443, \u043f\u043e\u0434\u043d\u044f\u0442\u0443\u044e \u043d\u0430 CppCon 2018. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0435\u0440\u0435\u0440\u044b\u0432\u0430 \u0432 \u043c\u043e\u0435\u043c \u0441\u0435\u043c\u0438\u043d\u0430\u0440\u0435, \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0443, \u044f \u043f\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043b \u0441 \u041d\u0438\u043a\u043e\u043b\u0430\u0435\u043c (\u0419\u043e\u0441\u0443\u0442\u0442\u0438\u0441\u043e\u043c). \u041e\u043d \u0441\u043f\u0440\u043e\u0441\u0438\u043b \u043c\u0435\u043d\u044f, \u0447\u0442\u043e \u044f \u0434\u0443\u043c\u0430\u044e \u043e \u043d\u043e\u0432\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438 <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2018\/p0660r5.pdf\">P0660: Cooperatively Interruptible Joining Thread<\/a>. \u041d\u0430 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u044f \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u043b \u043e\u0431 \u044d\u0442\u043e\u043c \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u041d\u0438\u043a\u043e\u043b\u0430\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f (\u043d\u0430\u0440\u044f\u0434\u0443 \u0441 \u0425\u0435\u0440\u0431\u043e\u043c \u0421\u0430\u0442\u0442\u0435\u0440\u043e\u043c \u0438 \u042d\u043d\u0442\u043e\u043d\u0438 \u0423\u0438\u043b\u044c\u044f\u043c\u0441\u043e\u043c). \u0421\u0435\u0433\u043e\u0434\u043d\u044f\u0448\u043d\u044f\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u043c\u0443 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0432 C++. \u041d\u0438\u0436\u0435 \u044f \u043f\u0440\u0438\u0432\u0435\u043b \u043e\u0431\u0449\u0443\u044e \u043a\u0430\u0440\u0442\u0438\u043d\u0443 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0438 \u0433\u0440\u044f\u0434\u0443\u0449\u0435\u043c C++.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2018\/p0660r5.pdf\">Cooperatively Interruptible Joining Thread<\/a> (\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u043c\u044b\u0439 \u043f\u043e\u0442\u043e\u043a) \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043d\u043e\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438\u043c\u0435\u0435\u0442 \u0434\u0432\u0435 \u043d\u043e\u0432\u044b\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438: \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u043c\u043e\u0441\u0442\u044c (interruptible) \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 (automatically joining, \u0437\u0434\u0435\u0441\u044c \u0438 \u0434\u0430\u043b\u0435\u0435 \u00ab\u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u00bb \u2014 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0434\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 <code>join()<\/code> \u2014 \u043f\u0440\u0438\u043c. \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0447\u0438\u043a\u0430). \u041f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u0430\u043c \u043e\u0431 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438.<\/p>\n<h3>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435<\/h3>\n<p>\u042d\u0442\u043e \u043d\u0435\u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 <code>std::thread<\/code>. \u0415\u0441\u043b\u0438 <code>std::thread<\/code> \u0432\u0441\u0435 \u0435\u0449\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>joinable<\/code>, \u0442\u043e \u0432 \u0435\u0433\u043e \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <code>std::terminate<\/code>. \u041f\u043e\u0442\u043e\u043a <code>thr<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>joinable<\/code>, \u0435\u0441\u043b\u0438 \u043d\u0438 <code>thr.join()<\/code>, \u043d\u0438 <code>thr.detach()<\/code> \u0435\u0449\u0435 \u043d\u0435 \u0431\u044b\u043b\u0438 \u0432\u044b\u0437\u0432\u0430\u043d\u044b.<\/p>\n<pre><code class=\"cpp\">\/\/ threadJoinable.cpp  #include &lt;iostream&gt; #include &lt;thread&gt;  int main(){          std::cout &lt;&lt; std::endl;     std::cout &lt;&lt; std::boolalpha;          std::thread thr{[]{ std::cout &lt;&lt; \"Joinable std::thread\" &lt;&lt; std::endl; }};          std::cout &lt;&lt; \"thr.joinable(): \" &lt;&lt; thr.joinable() &lt;&lt; std::endl;          std::cout &lt;&lt; std::endl;      }<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041e\u0431\u0430 \u043f\u043e\u0442\u043e\u043a\u0430 \u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u0443\u044e\u0442\u0441\u044f. \u041d\u0430 \u0432\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u043e\u0442\u043e\u043a <code>th<\/code> \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0441\u0432\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435: <code>\u00abJoinable std::thread\u00bb<\/code>.<\/p>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u044f \u0437\u0430\u043c\u0435\u043d\u044f\u044e \u0445\u0435\u0434\u0435\u0440 <code>&lt;thread&gt;<\/code> \u043d\u0430 &#171;<code>jthread.hpp<\/code>&#187; \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>std::jthread<\/code> \u0438\u0437 \u0433\u0440\u044f\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0430 C++.<\/p>\n<pre><code class=\"cpp\">\/\/ jthreadJoinable.cpp  #include &lt;iostream&gt; #include \"jthread.hpp\"  int main(){          std::cout &lt;&lt; std::endl;     std::cout &lt;&lt; std::boolalpha;          std::jthread thr{[]{ std::cout &lt;&lt; \"Joinable std::thread\" &lt;&lt; std::endl; }};          std::cout &lt;&lt; \"thr.joinable(): \" &lt;&lt; thr.joinable() &lt;&lt; std::endl;          std::cout &lt;&lt; std::endl;      }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0442\u043e\u043a <code>thr<\/code> \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0441\u0432\u043e\u0435\u043c \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435, \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u0441\u0435 \u0435\u0449\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>joinable<\/code>, \u043a\u0430\u043a, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h3>\u041f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435 std::jthread<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u043e\u0442 \u0447\u0435\u0433\u043e \u043e\u0442\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0442\u044c\u0441\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044c\u0442\u0435 \u043c\u043d\u0435 \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440.<\/p>\n<pre><code class=\"cpp\">\/\/ interruptJthread.cpp  #include \"jthread.hpp\" #include &lt;chrono&gt; #include &lt;iostream&gt;  using namespace::std::literals;  int main(){          std::cout &lt;&lt; std::endl;          std::jthread nonInterruptable([]{                                   \/\/ (1)         int counter{0};         while (counter &lt; 10){             std::this_thread::sleep_for(0.2s);             std::cerr &lt;&lt; \"nonInterruptable: \" &lt;&lt; counter &lt;&lt; std::endl;              ++counter;         }     });          std::jthread interruptable([](std::interrupt_token itoken){         \/\/ (2)         int counter{0};         while (counter &lt; 10){             std::this_thread::sleep_for(0.2s);             if (itoken.is_interrupted()) return;                        \/\/ (3)             std::cerr &lt;&lt; \"interruptable: \" &lt;&lt; counter &lt;&lt; std::endl;              ++counter;         }     });          std::this_thread::sleep_for(1s);          std::cerr &lt;&lt; std::endl;     std::cerr &lt;&lt; \"Main thread interrupts both jthreads\" &lt;&lt; std:: endl;     nonInterruptable.interrupt();     interruptable.interrupt();                                          \/\/ (4)          std::cout &lt;&lt; std::endl;      }<\/code><\/pre>\n<p>\u042f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043b \u0432 main \u0434\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u0430, <code>nonInterruptable<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0442\u044c, \u0438 <code>interruptable<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e (\u0441\u0442\u0440\u043e\u043a\u0438 1 \u0438 2). \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u043f\u043e\u0442\u043e\u043a\u0430 <code>nonInterruptable<\/code>, \u043f\u043e\u0442\u043e\u043a <code>interruptable<\/code>, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 <code>std::interrupt_token<\/code> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0435\u0433\u043e \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 3, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0431\u044b\u043b \u043b\u0438 \u043e\u043d \u043f\u0440\u0435\u0440\u0432\u0430\u043d: <code>itoken.is_interrupted()<\/code>. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u0432 \u043b\u044f\u043c\u0431\u0434\u0435 \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 <code>return<\/code> \u0438, \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f. \u0412\u044b\u0437\u043e\u0432 <code>interruptable.interrupt()<\/code> (\u0441\u0442\u0440\u043e\u043a\u0430 4) \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0438\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0430. \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 <code>nonInterruptable.interrupt()<\/code> \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u0430 <code>nonInterruptable<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439, \u043a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442 \u0441\u0432\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u043e\u0442 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0442\u043e\u043a\u0435\u043d\u0430\u0445 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f (interrupt tokens), \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0442\u043e\u043a\u0430\u0445 \u0438 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445.<\/p>\n<h4>\u0422\u043e\u043a\u0435\u043d\u044b \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f<\/h4>\n<p>\u0422\u043e\u043a\u0435\u043d \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f <code>std::interrupt_token<\/code> \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u0435\u0442 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0435 \u0432\u043b\u0430\u0434\u0435\u043d\u0438\u0435 (shared ownership) \u0438 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438, \u0435\u0441\u043b\u0438 \u0442\u043e\u043a\u0435\u043d \u0432\u0430\u043b\u0438\u0434\u0435\u043d. \u041e\u043d \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0430: <code>valid<\/code>, <code>is_interrupted<\/code>, \u0438 <code>interrupt<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><code>itoken.valid() \u2014 true<\/code>, \u0435\u0441\u043b\u0438 \u0442\u043e\u043a\u0435\u043d \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d \u0434\u043b\u044f \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438<\/p>\n<p><code>itoken.is_interrupted() \u2014 true<\/code>, \u0435\u0441\u043b\u0438 \u0431\u044b\u043b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u0441 <code>true<\/code> \u0438\u043b\u0438 \u0431\u044b\u043b \u0432\u044b\u0437\u0432\u0430\u043d \u043c\u0435\u0442\u043e\u0434\u0430 <code>interrupt()<\/code><\/p>\n<p><code>itoken.interrupt()<\/code> \u2014 \u0435\u0441\u043b\u0438 <code>!valid()<\/code> \u0438\u043b\u0438 <code>is_interrupted()<\/code>, \u0442\u043e \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0435 \u0432\u043e\u0437\u044b\u043c\u0435\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0430. \u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438 \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c <code>itoken.is_interrupted() == true<\/code>. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>is_interrupted()<\/code><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0442\u043e\u043a\u0435\u043d \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u043c \u0442\u043e\u043a\u0435\u043d\u043e\u043c. \u0414\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0439 \u0442\u043e\u043a\u0435\u043d \u043d\u0435 \u0432\u0430\u043b\u0438\u0434\u0435\u043d. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442, \u043a\u0430\u043a \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0438 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u043b\u044b.<\/p>\n<pre><code class=\"cpp\">std::jthread jthr([](std::interrupt_token itoken){     ...     std::interrupt_token interruptDisabled;      std::swap(itoken, interruptDisabled);     \/\/ (1)            ...     std::swap(itoken, interruptDisabled);     \/\/ (2)     ... }<\/code><\/pre>\n<p><code>std::interrupt_token interruptDisabled<\/code> \u043d\u0435 \u0432\u0430\u043b\u0438\u0434\u0435\u043d. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u044f\u0442\u044c \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 (1) \u0438 (2), \u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 (2) \u0443\u0436\u0435 \u043c\u043e\u0436\u0435\u0442.<\/p>\n<h4>\u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/h4>\n<p><code>std::jhread<\/code> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 <code>std::thread<\/code> \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u043c \u0441\u0438\u0433\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438 \u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435. \u0414\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c <code>std::interrupt_token<\/code>.<\/p>\n<h4>\u041d\u043e\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 Wait \u0434\u043b\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445<\/h4>\n<p>\u0414\u0432\u0435 \u0432\u0430\u0440\u0438\u0430\u0446\u0438\u0438 <code>wait wait_for<\/code> \u0438 <code>wait_until<\/code> \u0438\u0437 <code>std::condition_variable<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0442 \u043d\u043e\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438. \u041e\u043d\u0438 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0442 <code>std::interrupt_token<\/code>.<\/p>\n<pre><code class=\"cpp\">template &lt;class Predicate&gt; bool wait_until(unique_lock&lt;mutex&gt;&amp; lock,                  Predicate pred,                  interrupt_token itoken);  template &lt;class Rep, class Period, class Predicate&gt; bool wait_for(unique_lock&lt;mutex&gt;&amp; lock,                const chrono::duration&lt;Rep, Period&gt;&amp; rel_time,                Predicate pred,                interrupt_token itoken);  template &lt;class Clock, class Duration, class Predicate&gt; bool wait_until(unique_lock&lt;mutex&gt;&amp; lock,                  const chrono::time_point&lt;Clock, Duration&gt;&amp; abs_time,                  Predicate pred,                  interrupt_token itoken);<\/code><\/pre>\n<p>\u041d\u043e\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u043a\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0440\u0435\u0434\u0438\u043a\u0430\u0442. \u042d\u0442\u0438 \u0432\u0435\u0440\u0441\u0438\u0438 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0441\u0438\u0433\u043d\u0430\u043b \u043e \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0438 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u043c <code>std::interrupt_token itoken<\/code>. \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 <code>wait<\/code> \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043b\u0438 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043d\u0438\u0435.<\/p>\n<pre><code class=\"cpp\">cv.wait_until(lock, predicate, itoken); if (itoken.is_interrupted()){     \/\/ interrupt occurred }<\/code><\/pre>\n<h4>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435?<\/h4>\n<p>\u041a\u0430\u043a \u044f \u0438 \u043e\u0431\u0435\u0449\u0430\u043b \u0432 \u0441\u0432\u043e\u0435\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0441\u0432\u044f\u0449\u0435\u043d\u0430 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u043c\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0446\u0435\u043f\u0442\u043e\u0432 (concepts).<\/p>\n<hr>\n<blockquote>\n<p><em>\u0423\u0437\u043d\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043a\u0443\u0440\u0441\u0435 <\/em><a href=\"https:\/\/otus.pw\/Yf6l\/\"><em>&#171;<\/em><strong><em>C++ Developer. Professional<\/em><\/strong><em>&#171;<\/em><\/a><em>.<\/p>\n<p>\u0421\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u044c \u0434\u0435\u043c\u043e-\u0437\u0430\u043d\u044f\u0442\u0438\u044f \u043f\u043e \u0442\u0435\u043c\u0435 <\/em><a href=\"https:\/\/otus.pw\/WYur\/\"><strong><em>\u00ab\u041e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u043d\u0435\u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438\u00bb<\/em><\/strong><\/a><em>: \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u044d\u043a\u0441\u043f\u0435\u0440\u0442\u043e\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u0449\u0435\u0433\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e unit-\u0442\u0435\u0441\u0442\u043e\u0432 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c googletest.<\/em><\/p>\n<\/blockquote>\n<\/div>\n<p> \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\/company\/otus\/blog\/548316\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/548316\/<\/a><br \/><\/br><\/br><\/p><\/blockquote>\n<\/hr>\n<\/hr>\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-320032","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/320032","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=320032"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/320032\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=320032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=320032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=320032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}