{"id":330851,"date":"2022-03-20T09:00:13","date_gmt":"2022-03-20T09:00:13","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=330851"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=330851","title":{"rendered":"<span>\u041a\u0442\u043e \u0442\u0430\u043a\u043e\u0439 Thread Pool \u0438 \u043a\u0430\u043a \u0435\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0438\u043c\u0438 \u0440\u0443\u043a\u0430\u043c\u0438 \u043d\u0430 \u0421++<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u0414\u043b\u044f \u043a\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u044c\u044f?<\/h3>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u0434\u043b\u044f \u0442\u0435\u0445, \u0447\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 Thread Pool \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0438\u0432\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<code>\u0421++ 14<\/code>\u00a0\u0438\u00a0<code>\u0421++ 17<\/code>. \u0421\u0442\u043e\u0438\u0442 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0443\u0447\u0435\u0431\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0443\u0442 \u0434\u043b\u044f \u043a\u043e\u043c\u043c\u0435\u0440\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. <\/p>\n<h3>\u0427\u0442\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u044f \u043c\u043e\u0433\u0443 \u0443\u0437\u043d\u0430\u0442\u044c \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438?<\/h3>\n<ul>\n<li>\n<p>\u041a\u0442\u043e \u0442\u0430\u043a\u043e\u0439 Thread Pool?<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0447\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Thread Pool?<\/p>\n<\/li>\n<li>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b Thread Pool<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/basic_version.cpp\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <\/a>(\u0421++ 14)<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/best_version.cpp\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <\/a>(\u0421++ 17)<\/p>\n<\/li>\n<li>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439<\/p>\n<\/li>\n<\/ul>\n<h3>\u041a\u0442\u043e \u0442\u0430\u043a\u043e\u0439 Thread Pool?<\/h3>\n<p>\u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>\u043f\u0430\u0442\u0442\u0435\u0440\u043d<\/code>, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. Thread Pool \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0437 <em>\u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0437\u0430\u0434\u0430\u0447<\/em> \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u0441\u0442\u0430\u044e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u0438\u0445 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. \u0415\u0441\u0442\u044c \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043d\u043e \u043c\u044b \u0438\u0445 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c.<\/p>\n<h3>\u0417\u0430\u0447\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Thread Pool?<\/h3>\n<ul>\n<li>\n<p>\u041f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 (\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0430 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u044f\u0436\u0451\u043b\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439).<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<\/li>\n<\/ul>\n<h3>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0446\u0438\u0444\u0440<\/h3>\n<p>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0432 3 \u0441\u043b\u0443\u0447\u0430\u044f: \u0437\u0430\u043f\u0443\u0441\u043a \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0447\u0435\u0440\u0435\u0437\u00a0<code>std::thread<\/code>\u00a0\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<code>thread_pool<\/code>. \u041d\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435:  <\/p>\n<pre><code>void test_func(int&amp; res, const std::vector&lt;int>&amp; arr) {     res = 0;     for (int i = arr.size() - 1; i >= 0; --i) {         for (int j = 0; j &lt; arr.size(); ++j) {             res += arr[i] + arr[j];         }     } }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u0421\u043f\u043e\u0441\u043e\u0431 \u0437\u0430\u043f\u0443\u0441\u043a\u0430<\/p>\n<\/td>\n<td data-colwidth=\"223\" width=\"223\">\n<p>\u0412\u0440\u0435\u043c\u044f (\u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u044b)<\/p>\n<\/td>\n<td>\n<p>\u041a\u043e\u043b-\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u0411\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445  \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/p>\n<\/td>\n<td data-colwidth=\"223\" width=\"223\">\n<p>83954<\/p>\n<\/td>\n<td>\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><code>std::thred<\/code>  <\/p>\n<\/td>\n<td data-colwidth=\"223\" width=\"223\">\n<p>62386<\/p>\n<\/td>\n<td>\n<p>6  <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><code>thread_pool<\/code>  <\/p>\n<\/td>\n<td data-colwidth=\"223\" width=\"223\">\n<p>52474<\/p>\n<\/td>\n<td>\n<p>6<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0423 \u043c\u0435\u043d\u044f \u043d\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c 8 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u043b\u0441\u044f \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432 Visual Studio \u043d\u0430 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 Windows. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0444\u043e\u043d\u043e\u0432\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0444\u043b\u0443\u043a\u0442\u0443\u0430\u0446\u0438\u0438 \u0438 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0430. \u041a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c\u00a0<a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/basic_version.cpp\" rel=\"noopener noreferrer nofollow\">\u0422\u0423\u0422<\/a>\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u00a0<code>run_test<\/code> .<\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 6 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0435 \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u0438 \u043a\u043e\u0434 \u0432 6 \u0440\u0430\u0437?<\/h3>\n<p>\u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e Thread Pool \u0432 \u043c\u043e\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0434\u0435\u043b\u0430\u043d \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0442.\u043a. \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u00a0<code>condition_variable<\/code>, \u0447\u0442\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443, \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 (\u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e \u0437\u0430\u043a\u043e\u043d \u0410\u043c\u0434\u0430\u043b\u0430). \u041f\u043e \u043c\u0438\u043c\u043e \u0432\u044b\u0448\u0435\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0444\u0430\u043a\u0442\u043e\u0432 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043d\u0430 Windows, \u0447\u0442\u043e \u043d\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u0449\u0438\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u041f\u041a \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438.  <\/p>\n<h3>\u0425\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043a\u0430\u043a Thread Pool \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/h3>\n<p>Thread Pool \u0438\u043c\u0435\u0435\u0442 <em>\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0434\u0430\u0447<\/em>, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u043e\u0441\u0442\u0430\u0451\u0442 \u043d\u043e\u0432\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e <em>\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430 \u0438 \u043f\u043e\u0442\u043e\u043a \u0441\u0432\u043e\u0431\u043e\u0434\u0435\u043d<\/em>. \u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 Thread Pool \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435:  <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c09\/9d2\/cb8\/c099d2cb8d859f6f687c64afd35738f0.png\" width=\"11870\" height=\"2684\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c09\/9d2\/cb8\/c099d2cb8d859f6f687c64afd35738f0.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>\u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u0430\u0434\u0438\u044f:<\/strong>\u00a0\u0432\u0441\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b, \u0430 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 5 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/dbd\/030\/b48\/dbd030b48da224185403de52358c7720.png\" width=\"5935\" height=\"1827\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dbd\/030\/b48\/dbd030b48da224185403de52358c7720.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 1:<\/strong>\u00a0\u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432\u0437\u044f\u043b \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u0435\u0440\u0451\u0442 \u043f\u0435\u0440\u0432\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u044d\u0442\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u043a\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a <em>\u043e\u0431\u0449\u0435\u043c\u0443 \u0440\u0435\u0441\u0443\u0440\u0441\u0443<\/em> &#8212; \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u0412 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e 4 \u0438 5 \u0437\u0430\u0434\u0430\u0447\u0430 (\u0447\u0451\u0440\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438).  <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c51\/acc\/639\/c51acc639f3d308615b9b2068e51ce78.png\" width=\"5935\" height=\"2409\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c51\/acc\/639\/c51acc639f3d308615b9b2068e51ce78.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 2:<\/strong>\u00a0\u041d\u0430 3 \u0441\u0435\u043a\u0443\u043d\u0434\u0435 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 2 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0431\u0435\u0440\u0451\u0442 \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 (4 \u0437\u0430\u0434\u0430\u0447\u0443). \u0412 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e 5 \u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7a2\/889\/0bc\/7a28890bc092ad5973e638f303ff3902.png\" width=\"5935\" height=\"2409\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7a2\/889\/0bc\/7a28890bc092ad5973e638f303ff3902.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 3:<\/strong>\u00a0\u0422\u0440\u0435\u0442\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b \u0437\u0430\u0434\u0430\u0447\u0443 3 \u0438 \u0432\u0437\u044f\u043b \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 (5 \u0437\u0430\u0434\u0430\u0447\u0443). \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u0442\u0430\u043b\u0430 \u043f\u0443\u0441\u0442\u043e\u0439, \u043d\u043e \u043d\u0430\u0448\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e66\/178\/3ab\/e661783abae6e0c33c27011fef441d0a.png\" width=\"5935\" height=\"2409\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e66\/178\/3ab\/e661783abae6e0c33c27011fef441d0a.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 4:<\/strong>\u00a0\u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0432\u043e\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043d\u0435 \u0431\u0435\u0440\u0451\u0442 \u043d\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u0442. \u043a. \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430). \u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u0437\u0430\u0434\u0430\u0447 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0442\u043e\u043a \u0443\u0436\u0435 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0438 \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0434\u0451\u0442 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a (\u043d\u043e \u043c\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u043a\u0430\u043a\u043e\u0439 \u0438\u043c\u0435\u043d\u043d\u043e) \u0441\u0440\u0430\u0437\u0443 \u043d\u0430\u0447\u043d\u0451\u0442 \u0435\u0451 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ae0\/d9a\/3c4\/ae0d9a3c4c39aa91fc7b0eda3282e472.png\" width=\"5935\" height=\"2409\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ae0\/d9a\/3c4\/ae0d9a3c4c39aa91fc7b0eda3282e472.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 5:<\/strong>\u00a0\u0422\u0440\u0435\u0442\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 5.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ac4\/ea8\/f77\/ac4ea8f77114607a60a51a3746bd2667.png\" width=\"5935\" height=\"2409\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ac4\/ea8\/f77\/ac4ea8f77114607a60a51a3746bd2667.png\"\/><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 6:<\/strong>\u00a0\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 4 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p><strong>\u0418\u0442\u043e\u0433:<\/strong>\u00a0Thread Pool \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b 5 \u0437\u0430\u0434\u0430\u0447 \u0437\u0430 11 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<h3>\u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u043c\u0435\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u0448 Thread Pool?<\/h3>\n<p>\u041c\u044b \u0443\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c \u0441 \u043e\u0431\u0449\u0438\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b Thread Pool, \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c \u043e \u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c Thread Pool \u0431\u0443\u0434\u0435\u0442 2 \u0442\u0438\u043f\u0430 \u0437\u0430\u0434\u0430\u0447:\u00a0<code>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435<\/code>\u00a0(\u0431\u0443\u0434\u0443\u0442 \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0430) \u0438\u00a0<code>\u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435<\/code>\u00a0(\u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043d\u0435 \u0442\u043e\u0440\u043c\u043e\u0437\u044f\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a).<\/p>\n<p>Thread Pool \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 <em>\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441<\/em>:  <\/p>\n<ul>\n<li>\n<p><code>init(num_threads)<\/code> &#8212; \u043c\u0435\u0442\u043e\u0434, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 <code>num_threads<\/code> \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0412 \u043d\u0430\u0448\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c <em>\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/em>.<\/p>\n<\/li>\n<li>\n<p><code>add_task(task_func, args)<\/code> &#8212; <em>\u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439<\/em> \u043c\u0435\u0442\u043e\u0434 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u041f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>task_func<\/code> \u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>args<\/code> \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>task_id<\/code> (\u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043d\u043e\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438).<\/p>\n<\/li>\n<li>\n<p><code>wait(task_id)<\/code> &#8212; <em>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em>, \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c <code>task_id<\/code>. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c <em>\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <\/em>(\u043c\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435), \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <code>void<\/code>.<\/p>\n<\/li>\n<li>\n<p><code>wait_all()<\/code> &#8212; <em>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em>, \u0434\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<\/li>\n<li>\n<p><code>calculated(task_id)<\/code> &#8212; <em>\u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em>, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0449\u0438\u0439 \u0431\u044b\u043b\u0430 \u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441 \u043d\u043e\u043c\u0435\u0440\u043e\u043c <code>task_id<\/code>.<\/p>\n<\/li>\n<li>\n<p><code>shotdown()<\/code> &#8212; <em>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em>, \u0434\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e\u0449\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0443 Thread Pool. \u0414\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>\u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/em> (\u0445\u043e\u0442\u044f \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438 \u043c\u0435\u0442\u043e\u0434).<\/p>\n<\/li>\n<\/ul>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 (C++ 14)<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <em>\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043d\u0430\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0435:<\/p>\n<pre><code>\/\/ \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0434\u0430\u0447 - \u0445\u0440\u0430\u043d\u0438\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e(\u0437\u0430\u0434\u0430\u0447\u0443), \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438 \u043d\u043e\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 std::queue&lt;std::pair&lt;std::future&lt;void>, int64_t>> q;   std::mutex q_mtx; std::condition_variable q_cv;  \/\/ \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 std::unordered_set&lt;int64_t> completed_task_ids;   std::condition_variable completed_task_ids_cv; std::mutex completed_task_ids_mtx;  std::vector&lt;std::thread> threads;  \/\/ \u0444\u043b\u0430\u0433 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b thread_pool std::atomic&lt;bool> quite{ false };  \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0445\u0440\u0430\u043d\u044f\u0449\u0430\u044f id \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0430\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 std::atomic&lt;int64_t> last_idx = 0;<\/code><\/pre>\n<p>\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0445\u0440\u0430\u043d\u0438\u0442\u00a0<code>std::future&lt;void><\/code>\u00a0&#8212; \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u0432\u0435\u0440\u043d\u0451\u0442 \u0442\u0438\u043f void, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00a0<code>std::future<\/code>\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0435 \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0430 \u043e\u0442\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0434\u043e \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u0430, \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u00a0<code>std::function&lt;void()><\/code>\u00a0(\u0442\u0430\u043a\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0442\u043e\u0436\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c).<\/p>\n<pre><code>thread_pool(uint32_t num_threads) {     threads.reserve(num_threads);     for (uint32_t i = 0; i &lt; num_threads; ++i) {         threads.emplace_back(&amp;thread_pool::run, this);     } }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434\u00a0<code>run<\/code>.<\/p>\n<pre><code>void run() {     while (!quite) {         std::unique_lock&lt;std::mutex> lock(q_mtx);                  \/\/ \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438, \u0442\u043e \u0431\u0435\u0440\u0451\u043c \u0437\u0430\u0434\u0430\u0447\u0443, \u0438\u043d\u0430\u0447\u0435 - \u0437\u0430\u0441\u044b\u043f\u0430\u0435\u043c         \/\/ \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0448\u043b\u0438 \u0432 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0442\u043e quite \u0431\u0443\u0434\u0435\u0442 true \u0438 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c          \/\/ \u0436\u0434\u0430\u0442\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u0432\u044b\u0439\u0434\u0435\u043c \u0438\u0437 \u0446\u0438\u043a\u043b\u0430         q_cv.wait(lock, [this]()->bool { return !q.empty() || quite; });          if (!q.empty()) {             auto elem = std::move(q.front());             q.pop();             lock.unlock();  \/\/ \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 std::future (\u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e)              elem.first.get();              std::lock_guard&lt;std::mutex> lock(completed_task_ids_mtx);                          \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u043c\u0435\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u044b\u0445             completed_task_ids.insert(elem.second);              \/\/ \u0434\u0435\u043b\u0430\u0435\u043c notify, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0431\u0443\u0434\u0438\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0438             completed_task_ids_cv.notify_all();         }     } }<\/code><\/pre>\n<p><code>condition_variable<\/code>\u00a0\u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u0435\u00a0<code>wait<\/code>\u00a0(q_cv) \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043c\u044c\u044e\u0442\u0435\u043a\u0441, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435, \u0435\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0432\u0435\u0440\u043d\u043e, \u0442\u043e \u043c\u044b \u0438\u0434\u0451\u043c \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u043a\u043e\u0434\u0443, \u0438\u043d\u0430\u0447\u0435 &#8212; \u0437\u0430\u0441\u044b\u043f\u0430\u0435\u043c, \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043c\u044c\u044e\u0442\u0435\u043a\u0441 \u0438 \u0436\u0434\u0451\u043c \u0432\u044b\u0437\u043e\u0432\u00a0<code>notify<\/code>\u00a0\u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 (\u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u00a0<code>notify<\/code>\u00a0\u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f &#8212; \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043c\u044c\u044e\u0442\u0435\u043a\u0441 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435). \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0431\u0435\u0440\u0451\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043e\u043d\u0438 \u043d\u0435 \u043a\u043e\u043d\u0447\u0430\u0442\u0441\u044f, \u0430 \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043d\u0447\u0430\u0442\u0441\u044f \u0438 \u043f\u0440\u0438\u0434\u0451\u0442 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043c\u044b \u0440\u0430\u0437\u0431\u0443\u0434\u0438\u043c \u043f\u043e\u0442\u043e\u043a.<\/p>\n<pre><code>template &lt;typename Func, typename ...Args> int64_t add_task(const Func&amp; task_func, Args&amp;&amp;... args) {     \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438     int64_t task_idx = last_idx++;      std::lock_guard&lt;std::mutex> q_lock(q_mtx);     q.emplace(std::async(std::launch::deferred, task_func, args...), task_idx);          \/\/ \u0434\u0435\u043b\u0430\u0435\u043c notify_one, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0441\u043d\u0443\u043b\u0441\u044f \u043e\u0434\u0438\u043d \u0441\u043f\u044f\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a (\u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u0435\u0441\u0442\u044c)     \/\/ \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 run     q_cv.notify_one();     return task_idx; }<\/code><\/pre>\n<p><code>std::async(std::launch::deferred, task_func, args...)<\/code>\u00a0\u0434\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u00a0<code>async<\/code>\u00a0\u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0443\u00a0 <code>std::launch::deferred<\/code>. \u041c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u0430\u043a \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u00a0<code>std::bind<\/code>\u00a0. \u041e\u0442\u043b\u0438\u0447\u0430\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u0442\u043e,\u00a0<code>bind<\/code>\u00a0\u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432\u0441\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442 \u043e\u0442\u00a0<code>std::async<\/code>.<\/p>\n<pre><code>void wait(int64_t task_id) {     std::unique_lock&lt;std::mutex> lock(completed_task_ids_mtx);          \/\/ \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0432\u044b\u0437\u043e\u0432\u0430 notify \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 run (\u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438)     completed_task_ids_cv.wait(lock, [this, task_id]()->bool {         return completed_task_ids.find(task_id) != completed_task_ids.end();      }); }  void wait_all() {     std::unique_lock&lt;std::mutex> lock(q_mtx);          \/\/ \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0432\u044b\u0437\u043e\u0432\u0430 notify \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 run (\u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438)     completed_task_ids_cv.wait(lock, [this]()->bool {         std::lock_guard&lt;std::mutex> task_lock(completed_task_ids_mtx);         return q.empty() &amp;&amp; last_idx == completed_task_ids.size();     }); }<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e\u00a0<code>wait_all<\/code>\u00a0\u0432\u043d\u0443\u0442\u0440\u0438\u00a0<code>wait<\/code>\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0435\u0449\u0451 \u043e\u0434\u043d\u0443 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 \u043f\u0443\u0441\u0442\u043e\u0442\u0443 (\u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c\u00a0<code>data race<\/code>).<\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e\u00a0<code>std::lock_guard<\/code>\u00a0\u0441\u0442\u043e\u0438\u0442 \u0442\u0430\u043c, \u0433\u0434\u0435 \u043d\u0435\u0442\u00a0<code>wait<\/code>\u00a0\u0434\u043b\u044f \u043c\u044c\u044e\u0442\u0435\u043a\u0441\u0430 \u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c\u00a0<code>unlock<\/code>\u00a0(<code>std::unique_lock<\/code>\u00a0\u0432 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445). \u0415\u0441\u043b\u0438 \u0432\u044b \u0431\u0443\u0434\u0438\u0442\u0435 \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430, \u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b, \u0441\u043c\u043e\u0442\u0440\u044f\u0449\u0438\u0435 \u0432\u0430\u0448 \u043a\u043e\u0434 \u0441\u043a\u0430\u0436\u0443\u0442 \u0432\u0430\u043c \u0441\u043f\u0430\u0441\u0438\u0431\u043e.<\/p>\n<pre><code>bool calculated(int64_t task_id) {     std::lock_guard&lt;std::mutex> lock(completed_task_ids_mtx);     if (completed_task_ids.find(task_id) != completed_task_ids.end()) {         return true;     }     return false; }<\/code><\/pre>\n<p><em>\u041d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em> \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u043e\u0441\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u00a0<code>true<\/code>\u00a0\u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u00a0<code>task_id<\/code>\u00a0\u0443\u0436\u0435 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043d\u0430, \u0438\u043d\u0430\u0447\u0435 &#8212;\u00a0<code>false<\/code>.<\/p>\n<pre><code>~thread_pool() {     \/\/ \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c wait_all() \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447\u044c \u043f\u0435\u0440\u0435\u0434 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c     quite = true;     for (uint32_t i = 0; i &lt; threads.size(); ++i) {         q_cv.notify_all();         threads[i].join();     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430\u00a0<code>thread_pool<\/code>\u00a0\u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f, \u0442\u043e \u043c\u044b \u0434\u043e\u0436\u0438\u0434\u0430\u0435\u043c\u0441\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u0435\u0441\u043b\u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0435\u0441\u0442\u044c \u0435\u0449\u0451 \u0437\u0430\u0434\u0430\u0447\u0438, \u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0435\u0449\u0451 \u043e\u0434\u043d\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 (\u044d\u0442\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043e\u0436\u0438\u0434\u0430\u0442\u044c\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447 \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435\u043c).<\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/basic_version.cpp\" rel=\"noopener noreferrer nofollow\">\u0422\u0423\u0422<\/a>.<\/p>\n<h2>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Thread Pool <\/h2>\n<pre><code>void sum(int&amp; ans, std::vector&lt;int>&amp; arr) {     for (int i = 0; i &lt; arr.size(); ++i) {         ans += arr[i];     } }  int main() {     thread_pool tp(3);     std::vector&lt;int> s1 = { 1, 2, 3 };     int ans1 = 0;          std::vector&lt;int> s2 = { 4, 5 };     int ans2 = 0;          std::vector&lt;int> s3 = { 8, 9, 10 };     int ans3 = 0;      \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 thread_pool \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f 3 \u0437\u0430\u0434\u0430\u0447\u0438     auto id1 = tp.add_task(sum, std::ref(ans1), std::ref(s1));     auto id2 = tp.add_task(sum, std::ref(ans2), std::ref(s2));     auto id3 = tp.add_task(sum, std::ref(ans3), std::ref(s3));      if (tp.calculated(id1)) {         \/\/ \u0435\u0441\u043b\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0443\u0436\u0435 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043d, \u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043e\u0442\u0432\u0435\u0442         std::cout &lt;&lt; ans1 &lt;&lt; std::endl;     }     else {         \/\/ \u0435\u0441\u043b\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0435\u0449\u0451 \u043d\u0435 \u0433\u043e\u0442\u043e\u0432, \u0442\u043e \u0436\u0434\u0451\u043c \u0435\u0433\u043e         tp.wait(id1);         std::cout &lt;&lt; ans1 &lt;&lt; std::endl;     }     tp.wait_all();      std::cout &lt;&lt; ans2 &lt;&lt; std::endl;     std::cout &lt;&lt; ans3 &lt;&lt; std::endl;     return 0; }<\/code><\/pre>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430\u00a0<code>std::ref<\/code>\u00a0\u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0435\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u0430 \u0441\u0441\u044b\u043b\u043a\u0430, \u0430 \u043d\u0435 \u043a\u043e\u043f\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (\u044d\u0442\u043e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0432\u00a0<code>std::future<\/code>).<\/p>\n<p>\u0422\u0443\u0442 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 Thread Pool. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u044d\u0442\u043e\u0442 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u0438 \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c.<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u2116<\/p>\n<\/td>\n<td>\n<p>\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a<\/p>\n<\/td>\n<td>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>1<\/p>\n<\/td>\n<td>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c void<\/p>\n<\/td>\n<td>\n<p>\u041f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u043c\u0435\u043d\u044f\u0442\u044c \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0430 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>2<\/p>\n<\/td>\n<td>\n<p>\u041f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0434\u043b\u044f \u043e\u0442\u0432\u0435\u0442\u0430<\/p>\n<\/td>\n<td>\n<p>\u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438\u0437 thread_pool, \u0442\u043e \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0441 \u0441\u043e\u0431\u043e\u0439 \u0442\u0430\u0441\u043a\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435. \u0410 \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e 100 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 &#8230; ?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438\u00a0<code>C++ 14<\/code>, \u043d\u043e \u0437\u0430\u0442\u043e  <code>C++ 17<\/code>\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0445 \u0432\u044b\u0448\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u043e\u0432.<\/p>\n<h3>\u0423\u043b\u0443\u0447\u0448\u0430\u0435\u043c Thread Pool \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e C++ 17<\/h3>\n<p>\u0427\u0442\u043e\u0431\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043d\u0430\u0448\u0443 \u0432\u0435\u0440\u0441\u0438\u044e \u043d\u0443\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043d\u044f\u0442\u044c \u0432 \u0447\u0451\u043c \u0431\u044b\u043b\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0431\u044b\u043b\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0443\u0437\u043d\u0430\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 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u0443\u043c\u0435\u0442\u044c \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 (\u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f) \u0432 1 \u043e\u0431\u044a\u0435\u043a\u0442 \u0438 \u0442\u0443\u0442 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u00a0<code>std::any<\/code>.  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043d\u0430\u0448\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\u00a0<code>std::function&lt;std::any()><\/code> (\u0437\u0430\u043f\u0438\u0441\u044c\u00a0<code>std::future&lt;std::any><\/code>\u043d\u0435 \u0432\u0430\u043b\u0438\u0434\u043d\u0430) . \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u044f \u0438 \u0441\u0434\u0435\u043b\u0430\u043b \u0432 \u0441\u0432\u043e\u0435\u0439 \u043f\u0435\u0440\u0432\u043e\u0439 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043e\u0447\u0435\u043d\u044c <a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/void_func_problem.cpp\" rel=\"noopener noreferrer nofollow\">\u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0439 \u043a\u043e\u0434<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u043b\u0441\u044f \u043e\u0442 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e \u0442\u0443\u0442 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439, \u0447\u0442\u043e\u00a0<code>std::any<\/code>\u00a0\u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u0438\u043f\u0430\u00a0<code>void<\/code>\u00a0. \u0422\u043e\u0433\u0434\u0430 \u044f \u0440\u0435\u0448\u0438\u043b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u00a0<code>Task<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u044b \u0441\u043c\u043e\u0433 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043e\u0434\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u00a0<code>std::function&lt;std::any()><\/code>\u00a0\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u043c\u00a0<code>std::function&lt;void()><\/code>. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0433\u043e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440:<\/p>\n<pre><code>template &lt;typename FuncRetType, typename ...Args, typename ...FuncTypes> Task(FuncRetType(*func)(FuncTypes...), Args&amp;&amp;... args) :     is_void{ std::is_void_v&lt;FuncRetType> } {      if constexpr (std::is_void_v&lt;FuncRetType>) {         void_func = std::bind(func, args...);         any_func = []()->int { return 0; };     }     else {         void_func = []()->void {};         any_func = std::bind(func, args...);     } }<\/code><\/pre>\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u00a0<code>if constexpr<\/code>\u00a0\u0434\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0442\u043a\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u044f. \u0415\u0441\u043b\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u044b\u0439\u00a0<code>if<\/code>, \u0442\u043e \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0435\u0439\u00a0<code>void<\/code>\u00a0\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c\u00a0<code>void<\/code>\u00a0\u0432\u00a0<code>std::any<\/code>\u00a0\u0438 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u0430, \u043d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043a\u0430\u0441\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0432\u0435\u0442\u043a\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u00a0<code>typename ...Args<\/code>\u00a0\u0438\u00a0<code>typename ...FuncTypes<\/code>, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b \u0432\u043e\u0437\u043c\u043e\u0436\u0435\u043d \u043d\u0435\u044f\u0432\u043d\u044b\u0439 \u043a\u0430\u0441\u0442 \u043c\u0435\u0436\u0434\u0443\u00a0<code>std::referense_wrapper<\/code>\u00a0\u0438 \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u043c \u0442\u0438\u043f\u043e\u043c, \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u043d\u0435 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435 \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u00a0<code>std::referense_wrapper<\/code>.<\/p>\n<p><code>any_func = []()->int { return 0; };<\/code>\u00a0\u0438\u00a0<code>void_func = []()->void {};<\/code>\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u0437\u0430\u0433\u043b\u0443\u0448\u043a\u0438. \u041e\u043d\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043b\u0438\u0448\u043d\u0435\u0433\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043f\u0440\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f:<\/p>\n<pre><code>void operator() () {     void_func();     any_func_result = any_func(); }<\/code><\/pre>\n<p><code>has_result<\/code>\u00a0\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0432\u0435\u0440\u043d\u0451\u0442 \u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u043d\u0435\u0442, \u0430\u00a0<code>get_result<\/code>\u00a0\u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0435\u0433\u043e.<\/p>\n<pre><code>bool has_result() {     return !is_void; }  std::any get_result() const {     assert(!is_void);     assert(any_func_result.has_value());     return any_func_result; }<\/code><\/pre>\n<p>\u0415\u0449\u0451 \u043e\u0434\u0438\u043d \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441:\u00a0<code>TaskInfo<\/code>:  <\/p>\n<pre><code>enum class TaskStatus {     in_q,     completed };  struct TaskInfo {     TaskStatus status = TaskStatus::in_q;     std::any result; };<\/code><\/pre>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0445\u0440\u0430\u043d\u0438\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0437\u0430\u0434\u0430\u0447\u0435: \u0441\u0442\u0430\u0442\u0443\u0441 \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0415\u0441\u043b\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u00a0<code>void<\/code>, \u0442\u043e \u043f\u043e\u043b\u0435\u00a0<code>result<\/code>\u00a0\u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043d\u0435\u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u043c.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0435 \u043f\u043e\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430\u00a0<code>thread_pool<\/code> <\/p>\n<pre><code>std::vector&lt;std::thread> threads;  \/\/ \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043f\u0430\u0440\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0430, \u043d\u043e\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 std::queue&lt;std::pair&lt;Task, uint64_t>> q;  std::mutex q_mtx; std::condition_variable q_cv;  \/\/ \u0411\u0443\u0434\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447 \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0448\u043b\u0430 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0435\u0451 \u0441\u0442\u0430\u0442\u0443\u0441 \/\/ \u043f\u0440\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0438 std::unordered_map&lt;uint64_t, TaskInfo> tasks_info;  std::condition_variable tasks_info_cv; std::mutex tasks_info_mtx;  std::condition_variable wait_all_cv;  std::atomic&lt;bool> quite{ false }; std::atomic&lt;uint64_t> last_idx{ 0 };  \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u0447\u0438\u0442\u0430\u044e\u0449\u0430\u044f \u043a\u043e\u043b-\u0432\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 std::atomic&lt;uint64_t> cnt_completed_tasks{ 0 };<\/code><\/pre>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f\u00a0<code>cnt_completed_tasks<\/code>\u00a0(\u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0434\u043b\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u043a\u043e\u043b-\u0432\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u043b\u0438 \u043f\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0443 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430), \u0434\u043b\u044f \u043f\u043e\u0434\u0441\u0447\u0451\u0442\u0430 \u043a\u043e\u043b-\u0432\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447. \u042d\u0442\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<code>wait_all<\/code>\u00a0\u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0432\u0441\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0438\u0441\u044c.<\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c 3 \u0440\u0430\u0437\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430:<\/p>\n<pre><code>void wait(const uint64_t task_id) {     std::unique_lock&lt;std::mutex> lock(tasks_info_mtx);     tasks_info_cv.wait(lock, [this, task_id]()->bool {         return task_id &lt; last_idx&amp;&amp; tasks_info[task_id].status == TaskStatus::completed;     }); }  std::any wait_result(const uint64_t task_id) {     wait(task_id);     return tasks_info[task_id].result; }  template&lt;class T> void wait_result(const uint64_t task_id, T&amp; value) {     wait(task_id);     value = std::any_cast&lt;T>(tasks_info[task_id].result); }<\/code><\/pre>\n<ul>\n<li>\n<p><code>void wait(const uint64_t task_id)<\/code> &#8212; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0438 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>void<\/code>.<\/p>\n<\/li>\n<li>\n<p><code>std::any wait_result(const uint64_t task_id)<\/code> \u0438 <code>void wait_result(const uint64_t task_id, T&amp; value)<\/code> \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.<\/p>\n<\/li>\n<\/ul>\n<p><code>std::any wait_result(const uint64_t task_id)<\/code>\u00a0\u0432\u0435\u0440\u043d\u0451\u0442\u00a0<code>std::any<\/code>\u00a0\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0441\u0430\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c\u00a0<code>cast<\/code>\u00a0\u043a \u043d\u0443\u0436\u043d\u043e\u043c\u0443 \u0442\u0438\u043f\u0443. \u0428\u0430\u0431\u043b\u043e\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u00a0<code>void wait_result(const uint64_t task_id, T&amp; value)<\/code>\u00a0\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0432\u0442\u043e\u0440\u044b\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e, \u043a\u0443\u0434\u0430 \u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043e \u043d\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u044f\u0432\u043d\u044b\u0439\u00a0<code>cast<\/code>\u00a0\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c.<\/p>\n<p>\u0412 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u043c \u043a\u043e\u0434 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0438 \u043a\u043e\u0434 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438\u00a0<a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/best_version.cpp\" rel=\"noopener noreferrer nofollow\">\u0422\u0423\u0422<\/a>.<\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Thread Pool (\u0421++ 17)<\/h3>\n<pre><code>int int_sum(int a, int b) {     return a + b; }  void void_sum(int&amp; c, int a, int b) {     c = a + b; }  void void_without_argument() {     std::cout &lt;&lt; \"It's OK!\" &lt;&lt; std::endl; }  int main() {     thread_pool t(3);     int c;     t.add_task(int_sum, 2, 3);               \/\/ id = 0     t.add_task(void_sum, std::ref(c), 4, 6); \/\/ id = 1     t.add_task(void_without_argument);       \/\/ id = 2      {         \/\/ variant 1         int res;         t.wait_result(0, res);         std::cout &lt;&lt; res &lt;&lt; std::endl;          \/\/ variant 2         std::cout &lt;&lt; std::any_cast&lt;int>(t.wait_result(0)) &lt;&lt; std::endl;     }      t.wait(1);     std::cout &lt;&lt; c &lt;&lt; std::endl;      t.wait_all(); \/\/ waiting for task with id 2      return 0; }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b 2 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u044e\u00a0<code>wait_result<\/code>. \u041c\u043d\u0435 \u043b\u0438\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f 2 \u0432\u0430\u0440\u0438\u0430\u043d\u0442. \u041d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0430\u0441\u0442, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0430 \u0442\u0430\u043a \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0439\u043c\u0430\u0442\u044c \u0438 \u043e\u0442\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0448\u0438\u0431\u043a\u0438.<\/p>\n<h3>\u0423 \u043d\u0430\u0441 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0430\u0441\u044c \u0432\u0435\u0440\u0441\u0438\u044f \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439?<\/h3>\n<p>\u0418 \u0434\u0430, \u0438 \u043d\u0435\u0442. \u041f\u043e\u0441\u043b\u0435 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b: <\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<td data-colwidth=\"405\" width=\"405\">\n<p>\u0422\u0438\u043f \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438<\/p>\n<\/td>\n<td data-colwidth=\"132\" width=\"132\">\n<p><code>thread_pool<\/code> c++ 14<\/p>\n<\/td>\n<td>\n<p><code>thread_pool<\/code> c++ 17<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"405\" width=\"405\">\n<p>\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f <code>void<\/code><\/p>\n<\/td>\n<td data-colwidth=\"132\" width=\"132\">\n<p align=\"center\">+<\/p>\n<\/td>\n<td>\n<p align=\"center\">+<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"405\" width=\"405\">\n<p>\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u0432\u0441\u0451 <em>\u043a\u0440\u043e\u043c\u0435<\/em> <code>void<\/code><\/p>\n<\/td>\n<td data-colwidth=\"132\" width=\"132\">\n<p align=\"center\">+<\/p>\n<\/td>\n<td>\n<p align=\"center\">&#8212;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"405\" width=\"405\">\n<p><code>std::bind<\/code><\/p>\n<\/td>\n<td data-colwidth=\"132\" width=\"132\">\n<p align=\"center\">&#8212;<\/p>\n<\/td>\n<td>\n<p align=\"center\">+<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td data-colwidth=\"405\" width=\"405\">\n<p>\u0444\u0443\u043d\u043a\u0442\u043e\u0440<\/p>\n<\/td>\n<td data-colwidth=\"132\" width=\"132\">\n<p align=\"center\">&#8212;<\/p>\n<\/td>\n<td>\n<p align=\"center\">+<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u043c \u0438\u00a0<code>std::bind<\/code>:<\/p>\n<pre><code>class Test { public:     void operator() () {         std::cout &lt;&lt; \"Working with functors!\\n\";     } };  void sum(int a, int b) {     std::cout &lt;&lt; a + b &lt;&lt; std::endl; }  int main() {     Test test;     auto res = std::bind(sum, 2, 3);      thread_pool t(3); \/\/ C++ 14     t.add_task(test);     t.add_task(res);     t.wait_all();      return 0; }<\/code><\/pre>\n<h3>\u0410 \u043f\u043e\u0447\u0435\u043c\u0443 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043b\u0443\u0447\u0448\u0435?<\/h3>\n<p>\u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c Thread Pool, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0430\u043c \u0441\u043c\u043e\u0436\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\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 \u0438 \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u00a0<code>Task<\/code>, \u043d\u043e \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u00a0<code>std::bind<\/code>\u00a0\u043d\u0435\u043b\u044c\u0437\u044f \u044f\u0432\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437\u00a0<code>std::invoke_result<\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043f\u043e\u0439\u0442\u0438 \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u0441\u0442\u0443\u043f\u043a\u0438.<\/p>\n<h3>\u0418\u0442\u043e\u0433<\/h3>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 2 \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0435\u0440\u0441\u0438\u0438\u00a0<code>thred_pool<\/code>. \u0421\u043b\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a\u0430\u044f \u0438\u0437 \u043d\u0438\u0445 \u043b\u0443\u0447\u0448\u0435. \u041c\u043d\u0435 \u043b\u0438\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u0432\u0435\u0440\u0441\u0438\u044f \u0441\u00a0<code>C++ 17<\/code>. \u041e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0435 \u0442\u0430\u0441\u043a\u0430\u0442\u044c \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 \u043c\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043a\u0430\u043a \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0430 \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u0441\u0451 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0435\u0431\u044f. \u0414\u0430, \u044d\u0442\u0430 \u0432\u0435\u0440\u0441\u0438\u044f \u0443\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043f\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0442\u043e\u0440\u043e\u0432 \u0438\u00a0<code>std::bind<\/code>\u00a0\u043d\u0435 \u0447\u0430\u0441\u0442\u0430\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u044f \u0438 \u0441\u0447\u0438\u0442\u0430\u044e \u043b\u0443\u0447\u0448\u0438\u043c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/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\/post\/656515\/\"> https:\/\/habr.com\/ru\/post\/656515\/<\/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_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u0414\u043b\u044f \u043a\u043e\u0433\u043e \u0441\u0442\u0430\u0442\u044c\u044f?<\/h3>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u0434\u043b\u044f \u0442\u0435\u0445, \u0447\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 Thread Pool \u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430\u0438\u0432\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<code>\u0421++ 14<\/code>\u00a0\u0438\u00a0<code>\u0421++ 17<\/code>. \u0421\u0442\u043e\u0438\u0442 \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0443\u0447\u0435\u0431\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0443\u0442 \u0434\u043b\u044f \u043a\u043e\u043c\u043c\u0435\u0440\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. <\/p>\n<h3>\u0427\u0442\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u044f \u043c\u043e\u0433\u0443 \u0443\u0437\u043d\u0430\u0442\u044c \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438?<\/h3>\n<ul>\n<li>\n<p>\u041a\u0442\u043e \u0442\u0430\u043a\u043e\u0439 Thread Pool?<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0447\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Thread Pool?<\/p>\n<\/li>\n<li>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b Thread Pool<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/basic_version.cpp\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <\/a>(\u0421++ 14)<\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/best_version.cpp\" rel=\"noopener noreferrer nofollow\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <\/a>(\u0421++ 17)<\/p>\n<\/li>\n<li>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439<\/p>\n<\/li>\n<\/ul>\n<h3>\u041a\u0442\u043e \u0442\u0430\u043a\u043e\u0439 Thread Pool?<\/h3>\n<p>\u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>\u043f\u0430\u0442\u0442\u0435\u0440\u043d<\/code>, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. Thread Pool \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0437 <em>\u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0437\u0430\u0434\u0430\u0447<\/em> \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u0441\u0442\u0430\u044e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u0438\u0445 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. \u0415\u0441\u0442\u044c \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u043d\u043e \u043c\u044b \u0438\u0445 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c.<\/p>\n<h3>\u0417\u0430\u0447\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Thread Pool?<\/h3>\n<ul>\n<li>\n<p>\u041f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 (\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0430 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u044f\u0436\u0451\u043b\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439).<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<\/li>\n<\/ul>\n<h3>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0446\u0438\u0444\u0440<\/h3>\n<p>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0432 3 \u0441\u043b\u0443\u0447\u0430\u044f: \u0437\u0430\u043f\u0443\u0441\u043a \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0447\u0435\u0440\u0435\u0437\u00a0<code>std::thread<\/code>\u00a0\u0438 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<code>thread_pool<\/code>. \u041d\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435:  <\/p>\n<pre><code>void test_func(int&amp; res, const std::vector&lt;int>&amp; arr) {     res = 0;     for (int i = arr.size() - 1; i >= 0; --i) {         for (int j = 0; j &lt; arr.size(); ++j) {             res += arr[i] + arr[j];         }     } }<\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u0421\u043f\u043e\u0441\u043e\u0431 \u0437\u0430\u043f\u0443\u0441\u043a\u0430<\/p>\n<\/td>\n<td data-colwidth=\"223\" width=\"223\">\n<p>\u0412\u0440\u0435\u043c\u044f (\u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u044b)<\/p>\n<\/td>\n<td>\n<p>\u041a\u043e\u043b-\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u0411\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445  \u043f\u043e\u0442\u043e\u043a\u043e\u0432<\/p>\n<\/td>\n<td data-colwidth=\"223\" width=\"223\">\n<p>83954<\/p>\n<\/td>\n<td>\n<p>1<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><code>std::thred<\/code>  <\/p>\n<\/td>\n<td data-colwidth=\"223\" width=\"223\">\n<p>62386<\/p>\n<\/td>\n<td>\n<p>6  <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p><code>thread_pool<\/code>  <\/p>\n<\/td>\n<td data-colwidth=\"223\" width=\"223\">\n<p>52474<\/p>\n<\/td>\n<td>\n<p>6<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0423 \u043c\u0435\u043d\u044f \u043d\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c 8 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u043b\u0441\u044f \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432 Visual Studio \u043d\u0430 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 Windows. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0444\u043e\u043d\u043e\u0432\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0444\u043b\u0443\u043a\u0442\u0443\u0430\u0446\u0438\u0438 \u0438 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0430. \u041a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c\u00a0<a href=\"https:\/\/github.com\/skprpi\/Habr\/blob\/main\/thread_pool\/basic_version.cpp\" rel=\"noopener noreferrer nofollow\">\u0422\u0423\u0422<\/a>\u00a0\u0444\u0443\u043d\u043a\u0446\u0438\u044f\u00a0<code>run_test<\/code> .<\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 6 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043d\u0435 \u0443\u0441\u043a\u043e\u0440\u0438\u043b\u0438 \u043a\u043e\u0434 \u0432 6 \u0440\u0430\u0437?<\/h3>\n<p>\u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e Thread Pool \u0432 \u043c\u043e\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0434\u0435\u043b\u0430\u043d \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0442.\u043a. \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u00a0<code>condition_variable<\/code>, \u0447\u0442\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u043c\u0435\u0434\u043b\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443, \u0442\u0430\u043a \u0436\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 (\u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e \u0437\u0430\u043a\u043e\u043d \u0410\u043c\u0434\u0430\u043b\u0430). \u041f\u043e \u043c\u0438\u043c\u043e \u0432\u044b\u0448\u0435\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0444\u0430\u043a\u0442\u043e\u0432 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043d\u0430 Windows, \u0447\u0442\u043e \u043d\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u0449\u0438\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u041f\u041a \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c\u0438.  <\/p>\n<h3>\u0425\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043a\u0430\u043a Thread Pool \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/h3>\n<p>Thread Pool \u0438\u043c\u0435\u0435\u0442 <em>\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0434\u0430\u0447<\/em>, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u043e\u0441\u0442\u0430\u0451\u0442 \u043d\u043e\u0432\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e <em>\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430 \u0438 \u043f\u043e\u0442\u043e\u043a \u0441\u0432\u043e\u0431\u043e\u0434\u0435\u043d<\/em>. \u0414\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0430\u0431\u043e\u0442\u0443 Thread Pool \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435:  <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u0430\u0434\u0438\u044f:<\/strong>\u00a0\u0432\u0441\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b, \u0430 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 5 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 1:<\/strong>\u00a0\u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432\u0437\u044f\u043b \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u0435\u0440\u0451\u0442 \u043f\u0435\u0440\u0432\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u044d\u0442\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u043a\u0442\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a <em>\u043e\u0431\u0449\u0435\u043c\u0443 \u0440\u0435\u0441\u0443\u0440\u0441\u0443<\/em> &#8212; \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u0412 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e 4 \u0438 5 \u0437\u0430\u0434\u0430\u0447\u0430 (\u0447\u0451\u0440\u043d\u044b\u043c \u0446\u0432\u0435\u0442\u043e\u043c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438).  <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 2:<\/strong>\u00a0\u041d\u0430 3 \u0441\u0435\u043a\u0443\u043d\u0434\u0435 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 2 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0431\u0435\u0440\u0451\u0442 \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 (4 \u0437\u0430\u0434\u0430\u0447\u0443). \u0412 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e 5 \u0437\u0430\u0434\u0430\u0447\u0430.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 3:<\/strong>\u00a0\u0422\u0440\u0435\u0442\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b \u0437\u0430\u0434\u0430\u0447\u0443 3 \u0438 \u0432\u0437\u044f\u043b \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 (5 \u0437\u0430\u0434\u0430\u0447\u0443). \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u0442\u0430\u043b\u0430 \u043f\u0443\u0441\u0442\u043e\u0439, \u043d\u043e \u043d\u0430\u0448\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 4:<\/strong>\u00a0\u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0441\u0432\u043e\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043d\u0435 \u0431\u0435\u0440\u0451\u0442 \u043d\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 (\u0442. \u043a. \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430). \u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u0437\u0430\u0434\u0430\u0447 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0442\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0442\u043e\u043a \u0443\u0436\u0435 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a \u0438 \u043a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0434\u0451\u0442 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a (\u043d\u043e \u043c\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u043c \u043a\u0430\u043a\u043e\u0439 \u0438\u043c\u0435\u043d\u043d\u043e) \u0441\u0440\u0430\u0437\u0443 \u043d\u0430\u0447\u043d\u0451\u0442 \u0435\u0451 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 5:<\/strong>\u00a0\u0422\u0440\u0435\u0442\u0438\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 5.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p><strong>\u0421\u0442\u0430\u0434\u0438\u044f 6:<\/strong>\u00a0\u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043b \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 4 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p><strong>\u0418\u0442\u043e\u0433:<\/strong>\u00a0Thread Pool \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b 5 \u0437\u0430\u0434\u0430\u0447 \u0437\u0430 11 \u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<h3>\u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0443\u043c\u0435\u0442\u044c \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430\u0448 Thread Pool?<\/h3>\n<p>\u041c\u044b \u0443\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c \u0441 \u043e\u0431\u0449\u0438\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b Thread Pool, \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c \u043e \u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c Thread Pool \u0431\u0443\u0434\u0435\u0442 2 \u0442\u0438\u043f\u0430 \u0437\u0430\u0434\u0430\u0447:\u00a0<code>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435<\/code>\u00a0(\u0431\u0443\u0434\u0443\u0442 \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043a\u043e\u043c\u0430\u043d\u0434\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0430) \u0438\u00a0<code>\u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435<\/code>\u00a0(\u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043d\u0435 \u0442\u043e\u0440\u043c\u043e\u0437\u044f\u0442 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a).<\/p>\n<p>Thread Pool \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 <em>\u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441<\/em>:  <\/p>\n<ul>\n<li>\n<p><code>init(num_threads)<\/code> &#8212; \u043c\u0435\u0442\u043e\u0434, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 <code>num_threads<\/code> \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u0412 \u043d\u0430\u0448\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c <em>\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/em>.<\/p>\n<\/li>\n<li>\n<p><code>add_task(task_func, args)<\/code> &#8212; <em>\u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439<\/em> \u043c\u0435\u0442\u043e\u0434 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u041f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>task_func<\/code> \u0438 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>args<\/code> \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 <code>task_id<\/code> (\u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u043d\u043e\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438).<\/p>\n<\/li>\n<li>\n<p><code>wait(task_id)<\/code> &#8212; <em>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em>, \u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c <code>task_id<\/code>. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c <em>\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <\/em>(\u043c\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435), \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c <code>void<\/code>.<\/p>\n<\/li>\n<li>\n<p><code>wait_all()<\/code> &#8212; <em>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em>, \u0434\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<\/li>\n<li>\n<p><code>calculated(task_id)<\/code> &#8212; <em>\u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em>, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0449\u0438\u0439 \u0431\u044b\u043b\u0430 \u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441 \u043d\u043e\u043c\u0435\u0440\u043e\u043c <code>task_id<\/code>.<\/p>\n<\/li>\n<li>\n<p><code>shotdown()<\/code> &#8212; <em>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em>, \u0434\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e\u0449\u0438\u0439 \u0440\u0430\u0431\u043e\u0442\u0443 Thread Pool. \u0414\u043b\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0433\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>\u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440<\/em> (\u0445\u043e\u0442\u044f \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438 \u043c\u0435\u0442\u043e\u0434).<\/p>\n<\/li>\n<\/ul>\n<h3>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 (C++ 14)<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <em>\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435<\/em>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043d\u0430\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0435:<\/p>\n<pre><code>\/\/ \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u0434\u0430\u0447 - \u0445\u0440\u0430\u043d\u0438\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e(\u0437\u0430\u0434\u0430\u0447\u0443), \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438 \u043d\u043e\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 std::queue&lt;std::pair&lt;std::future&lt;void>, int64_t>> q;   std::mutex q_mtx; std::condition_variable q_cv;  \/\/ \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 std::unordered_set&lt;int64_t> completed_task_ids;   std::condition_variable completed_task_ids_cv; std::mutex completed_task_ids_mtx;  std::vector&lt;std::thread> threads;  \/\/ \u0444\u043b\u0430\u0433 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b thread_pool std::atomic&lt;bool> quite{ false };  \/\/ \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0445\u0440\u0430\u043d\u044f\u0449\u0430\u044f id \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0434\u0430\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 std::atomic&lt;int64_t> last_idx = 0;<\/code><\/pre>\n<p>\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0445\u0440\u0430\u043d\u0438\u0442\u00a0<code>std::future&lt;void><\/code>\u00a0&#8212; \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u0432\u0435\u0440\u043d\u0451\u0442 \u0442\u0438\u043f void, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u00a0<code>std::future<\/code>\u00a0\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0435 \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0430 \u043e\u0442\u043b\u043e\u0436\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0434\u043e \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u0430, \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u00a0<code>std::function&lt;void()><\/code>\u00a0(\u0442\u0430\u043a\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0442\u043e\u0436\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c).<\/p>\n<pre><code>thread_pool(uint32_t num_threads) {     threads.reserve(num_threads);     for (uint32_t i = 0; i &lt; num_threads; ++i) {         threads.emplace_back(&amp;thread_pool::run, this);     } }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434\u00a0<code>run<\/code>.<\/p>\n<pre><code>void run() {     while (!quite) {         std::unique_lock&lt;std::mutex> lock(q_mtx);                  \/\/ \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438, \u0442\u043e \u0431\u0435\u0440\u0451\u043c \u0437\u0430\u0434\u0430\u0447\u0443, \u0438\u043d\u0430\u0447\u0435 - \u0437\u0430\u0441\u044b\u043f\u0430\u0435\u043c         \/\/ \u0435\u0441\u043b\u0438 \u043c\u044b \u0437\u0430\u0448\u043b\u0438 \u0432 \u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0442\u043e quite \u0431\u0443\u0434\u0435\u0442 true \u0438 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c          \/\/ \u0436\u0434\u0430\u0442\u044c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u0432\u044b\u0439\u0434\u0435\u043c \u0438\u0437 \u0446\u0438\u043a\u043b\u0430         q_cv.wait(lock, [this]()->bool { return !q.empty() || quite; });          if (!q.empty()) {             auto elem = std::move(q.front());             q.pop();             lock.unlock();  \/\/ \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0438\u043f\u0430 std::future (\u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e)              elem.first.get();              std::lock_guard&lt;std::mutex> lock(completed_task_ids_mtx);                          \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u043c\u0435\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u044b\u0445             completed_task_ids.insert(elem.second);              \/\/ \u0434\u0435\u043b\u0430\u0435\u043c notify, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0431\u0443\u0434\u0438\u0442\u044c \u043f\u043e\u0442\u043e\u043a\u0438             completed_task_ids_cv.notify_all();         }     } }<\/code><\/pre>\n<p><code>condition_variable<\/code>\u00a0\u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u0435\u00a0<code>wait<\/code>\u00a0(q_cv) \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043c\u044c\u044e\u0442\u0435\u043a\u0441, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435, \u0435\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0432\u0435\u0440\u043d\u043e, \u0442\u043e \u043c\u044b \u0438\u0434\u0451\u043c \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u043a\u043e\u0434\u0443, \u0438\u043d\u0430\u0447\u0435 &#8212; \u0437\u0430\u0441\u044b\u043f\u0430\u0435\u043c, \u043e\u0442\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043c\u044c\u044e\u0442\u0435\u043a\u0441 \u0438 \u0436\u0434\u0451\u043c \u0432\u044b\u0437\u043e\u0432\u00a0<code>notify<\/code>\u00a0\u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 (\u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u00a0<code>notify<\/code>\u00a0\u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f &#8212; \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043c\u044c\u044e\u0442\u0435\u043a\u0441 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435). \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u044b \u0431\u0435\u0440\u0451\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043e\u043d\u0438 \u043d\u0435 \u043a\u043e\u043d\u0447\u0430\u0442\u0441\u044f, \u0430 \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043d\u0447\u0430\u0442\u0441\u044f \u0438 \u043f\u0440\u0438\u0434\u0451\u0442 \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043c\u044b \u0440\u0430\u0437\u0431\u0443\u0434\u0438\u043c \u043f\u043e\u0442\u043e\u043a.<\/p>\n<pre><code>template &lt;typename Func, typename ...Args> int64_t add_task(const Func&amp; task_func, Args&amp;&amp;... args) {     \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438     int64_t task_idx = last_idx++;      std::lock_guard&lt;std::mutex> q_lock(q_mtx);     q.emplace(std::async(std::launch::deferred, task_func, args...), task_idx);          \/\/ \u0434\u0435\u043b\u0430\u0435\u043c notify_one, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0441\u043d\u0443\u043b\u0441\u044f \u043e\u0434\u0438\u043d \u0441\u043f\u044f\u0449\u0438\u0439 \u043f\u043e\u0442\u043e\u043a (\u0435\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0439 \u0435\u0441\u0442\u044c)     \/\/ \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 run     q_cv.notify_one();     return task_idx; }<\/code><\/pre>\n<p><code>std::async(std::launch::deferred, task_func, args...)<\/code>\u00a0\u0434\u0430\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u00a0<code>async<\/code>\u00a0\u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0443\u00a0 <code>std::launch::deferred<\/code>. \u041c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u0430\u043a \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u00a0<code>std::bind<\/code>\u00a0. \u041e\u0442\u043b\u0438\u0447\u0430\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u0442\u043e,\u00a0<code>bind<\/code>\u00a0\u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432\u0441\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0432 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442 \u043e\u0442\u00a0<code>std::async<\/code>.<\/p>\n<pre><code>void wait(int64_t task_id) {     std::unique_lock&lt;std::mutex> lock(completed_task_ids_mtx);          \/\/ \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0432\u044b\u0437\u043e\u0432\u0430 notify \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 run (\u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438)     completed_task_ids_cv.wait(lock, [this, task_id]()->bool {         return completed_task_ids.find(task_id) != completed_task_ids.end();      }); }  void wait_all() {     std::unique_lock&lt;std::mutex> lock(q_mtx);          \/\/ \u043e\u0436\u0438\u0434\u0430\u0435\u043c \u0432\u044b\u0437\u043e\u0432\u0430 notify \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 run (\u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438)     completed_task_ids_cv.wait(lock, [this]()->bool {         std::lock_guard&lt;std::mutex> task_lock(completed_task_ids_mtx);         return q.empty() &amp;&amp; last_idx == completed_task_ids.size();     }); }<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e\u00a0<code>wait_all<\/code>\u00a0\u0432\u043d\u0443\u0442\u0440\u0438\u00a0<code>wait<\/code>\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0435\u0449\u0451 \u043e\u0434\u043d\u0443 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 \u043f\u0443\u0441\u0442\u043e\u0442\u0443 (\u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c\u00a0<code>data race<\/code>).<\/p>\n<p>\u0422\u0430\u043a \u0436\u0435 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e\u00a0<code>std::lock_guard<\/code>\u00a0\u0441\u0442\u043e\u0438\u0442 \u0442\u0430\u043c, \u0433\u0434\u0435 \u043d\u0435\u0442\u00a0<code>wait<\/code>\u00a0\u0434\u043b\u044f \u043c\u044c\u044e\u0442\u0435\u043a\u0441\u0430 \u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c\u00a0<code>unlock<\/code>\u00a0(<code>std::unique_lock<\/code>\u00a0\u0432 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445). \u0415\u0441\u043b\u0438 \u0432\u044b \u0431\u0443\u0434\u0438\u0442\u0435 \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430, \u0442\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u044b, \u0441\u043c\u043e\u0442\u0440\u044f\u0449\u0438\u0435 \u0432\u0430\u0448 \u043a\u043e\u0434 \u0441\u043a\u0430\u0436\u0443\u0442 \u0432\u0430\u043c \u0441\u043f\u0430\u0441\u0438\u0431\u043e.<\/p>\n<pre><code>bool calculated(int64_t task_id) {     std::lock_guard&lt;std::mutex> lock(completed_task_ids_mtx);     if (completed_task_ids.find(task_id) != completed_task_ids.end()) {         return true;     }     return false; }<\/code><\/pre>\n<p><em>\u041d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434<\/em> \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u043e\u0441\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u00a0<code>true<\/code>\u00a0\u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u00a0<code>task_id<\/code>\u00a0\u0443\u0436\u0435 \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043d\u0430, \u0438\u043d\u0430\u0447\u0435 &#8212;\u00a0<code>false<\/code>.<\/p>\n<pre><code>~thread_pool() {     \/\/ \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c wait_all() \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0436\u0434\u0430\u0442\u044c\u0441\u044f \u0432\u0441\u0435\u0445<\/code><\/pre>\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-330851","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/330851","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=330851"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/330851\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}