{"id":186200,"date":"2013-07-10T22:01:04","date_gmt":"2013-07-10T18:01:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=186200"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=186200","title":{"rendered":"<span class=\"post_title\">\u041d\u0435 \u0431\u043e\u0439\u0442\u0435\u0441\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0432. \u0418\u043b\u0438 \u0435\u0449\u0435 \u043e\u0434\u0438\u043d GCD \u043d\u0430 C++11<\/span>"},"content":{"rendered":"<div class=\"content html_format\">\n<h4>\u0418\u041c\u0425\u041e (\u0418\u043c\u0435\u044e \u041c\u043d\u0435\u043d\u0438\u0435 \u0425\u0440\u0435\u043d \u041e\u0441\u043f\u043e\u0440\u0438\u0448\u044c)<\/h4>\n<p>  \u0421 \u043c\u043e\u0435\u0439 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u043e\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u0434\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u2014 \u044d\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0432. \u0412\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u2014 \u043e\u0447\u0435\u043d\u044c \u0443\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441. \u0418\u043d\u043e\u0433\u0434\u0430 \u043e\u043d \u0443\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0430, \u0440\u0430\u0434\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u0430\u043c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0438 \u0437\u0430\u0442\u0435\u0432\u0430\u043b\u0441\u044f. \u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430 (\u043f\u043e\u0434 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u043c \u044f \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e) \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0438 \u0442\u0435\u0445\u043d\u0438\u043a.<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/15a\/1bf\/b69\/15a1bfb6986f94d613aeaab475a50e86.jpg\"\/><br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f<\/h4>\n<p>  \u0412\u043e\u0442 \u0443\u0436\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0440\u0435\u0445 \u043b\u0435\u0442 \u043c\u043e\u0438\u043c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u0440\u0430\u0431\u043e\u0447\u0438\u043c \u044f\u0437\u044b\u043a\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f objective-c, \u0438 \u043a\u043e\u0433\u0434\u0430 \u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0430\u043b \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 \u043d\u0435\u043c \u043c\u0435\u043d\u044f \u043f\u0440\u0438\u044f\u0442\u043d\u043e \u0443\u0434\u0438\u0432\u0438\u043b \u043f\u0440\u043e\u0434\u0443\u043c\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0441\u043e\u043a\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439 API \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 NSOperationQueue, \u0430 \u043f\u043e\u0437\u0436\u0435 \u2014 GCD, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e \u043c\u043e\u0435\u043c\u0443 \u043c\u043d\u0435\u043d\u0438\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0432\u0438\u043d\u0442\u044d\u0441\u0441\u0435\u043d\u0446\u0438\u0435\u0439 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f Thread concurrency. \u0418 \u0432\u043e\u0442 \u043d\u0435\u0434\u0430\u0432\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u0425\u0430\u0431\u0440\u0435: <a href=\"http:\/\/habrahabr.ru\/post\/185706\/\">\u0422\u0435\u0445\u043d\u0438\u043a\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0430\u043d\u0430\u043b\u043e\u0433\u0430 await\/async \u0438\u0437 C# \u0434\u043b\u044f C++<\/a> \u0438 <a href=\"http:\/\/habrahabr.ru\/post\/184562\/\">Thread concurrency C++11<\/a>. \u041e\u043d\u0438 \u0438 \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0442\u0435 \u043d\u043e\u0432\u044b\u0435 \u043f\u043b\u044e\u0448\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 C++ \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. \u0418 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0438\u0437 \u043d\u0438\u0445 (\u0442\u043e\u0442-\u0436\u0435 std::future) \u0434\u043b\u044f \u043c\u0435\u043d\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/6f3\/1a3\/100\/6f31a31005c7e24ac4396e0660e00cc3.jpg\"\/><\/p>\n<h4>\u0414\u043e\u043c\u044b\u0441\u043b\u044b \u0438 \u0445\u043e\u0442\u0435\u043b\u043a\u0438<\/h4>\n<p>  \u0412\u043e\u0442 \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0439, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445:  <\/p>\n<ul>\n<li>\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 (\u0444\u0430\u0439\u043b\u044b\/\u0441\u0435\u0442\u044c);<\/li>\n<li>\u0440\u0430\u0441\u043f\u0430\u0440\u0441\u0438\u0442\u044c\/\u043f\u043e\u0434\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435;<\/li>\n<li>\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u043f\u043e\u0442\u043e\u043a (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c UI).<\/li>\n<\/ul>\n<p>  \u0423\u0434\u043e\u0431\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0437 \u044d\u0442\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0431\u044b\u043b\u0430 \u0441\u0432\u043e\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c.<br \/>  \u0410 \u0435\u0449\u0435 \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u044d\u0442\u043e \u0441\u043e\u0431\u0440\u0430\u043d\u043e \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435, \u0430 \u043d\u0435 \u0440\u0430\u0437\u0431\u0440\u043e\u0441\u0430\u043d\u043e \u043f\u043e \u043f\u044f\u0442\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u0444\u0430\u0439\u043b\u0430\u043c. \u0427\u0442\u043e-\u0442\u043e \u043d\u0430 \u043f\u043e\u0434\u043e\u0431\u0438\u0438:  <\/p>\n<pre><code class=\"cpp\">file_io_queue.async([=]{     file_data = get_data_from_file( file_name );     parser_queue.async([=]{         parsed_data = parse( file_data );         main_queue.async([=]{             update_ui_with_new_data( parsed_data ) ;         });     }); });<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439, \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434. \u041e\u043d \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043b\u043e\u0433\u0438\u043a\u0443 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0434\u0430\u043d\u043d\u044b\u0445. \u0414\u043b\u044f \u043c\u0435\u043d\u044f, \u043f\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u043c\u0443 \u0441\u0447\u0435\u0442\u0443, \u0431\u0435\u0437 \u0440\u0430\u0437\u043d\u0438\u0446\u044b, \u0432 \u043a\u0430\u043a\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0432\u044b\u0447\u0438\u0442\u043a\u0430 \u0444\u0430\u0439\u043b\u0430, \u0432 \u043a\u0430\u043a\u043e\u043c \u2014 \u0435\u0433\u043e \u043f\u0430\u0440\u0441\u0438\u043d\u0433. \u0413\u043b\u0430\u0432\u043d\u043e\u0435 \u2014 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u042f \u043c\u043e\u0433\u0443 \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u043a\u043e\u0434 100500 \u0440\u0430\u0437 \u0434\u043b\u044f 100500 \u0444\u0430\u0439\u043b\u043e\u0432.<\/p>\n<p>  \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u2014 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u043f\u0443\u043b\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u041d\u043e \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u0432\u0438\u0434\u0435\u043d\u043d\u044b\u0435 \u043c\u043d\u043e\u0439 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0440\u0430\u0445 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0438\u043a\u043e\u0432, \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d std::thread \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u0421 \u043c\u043e\u0435\u0439 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u044d\u0442\u043e \u043d\u0435 \u0435\u0441\u0442\u044c \u0445\u043e\u0440\u043e\u0448\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0443\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u043d\u0441\u0442\u0430\u043d\u0441 \u0441\u0430\u043c\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432\u0441\u0435 \u0432\u0440\u0435\u043c\u044f, \u043f\u043e\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u0441\u0442\u0442\u0430\u043d\u0441\u0430 std::thread \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043a\u0438 \u0431\u043e\u043b\u0435\u0435 \u0437\u0430\u0442\u0440\u0430\u0442\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u0447\u0435\u043c \u0437\u0430\u0445\u0432\u0430\u0442\/\u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043c\u044e\u0442\u0435\u043a\u0441\u0430. \u041a\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u0441\u0442\u043e\u0438\u0442 \u0443\u043d\u0438\u0447\u0442\u043e\u0436\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c? \u0414\u0430 \u0438 \u043f\u0440\u043e\u0441\u0442\u0430\u0438\u0432\u0430\u044e\u0449\u0435\u0435 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f, \u043a\u043e\u0433\u0434\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u2014 \u043d\u0435 \u0430\u0439\u0441. <br \/>  \u041c\u044b \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u043e \u0434\u0440\u0443\u0433\u043e\u043c\u0443. \u0423 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 N-\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u0442\u043e\u043a\u043e\u0432 (std::thread) \u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u043b\u0435\u0433\u043a\u043e\u0432\u0435\u0441\u043d\u044b\u0445 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439 \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430\u043c\u0438. \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0442\u043e \u043e\u043f\u043e\u0432\u0435\u0449\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a \u043e \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430. \u041f\u043e\u0442\u043e\u043a \u0431\u0435\u0440\u0435\u0442 \u0441\u0430\u043c\u0443\u044e \u0432\u044b\u0441\u043e\u043a\u043e\u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0435\u0435. \u0415\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441 \u0442\u0430\u043a\u0438\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c \u0443\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u0442\u043e \u0431\u0435\u0440\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u043d\u0438\u0437\u043a\u043e\u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443. \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u0438\u0445 \u043d\u0435\u0442 \u2014 \u0436\u0434\u0435\u0442.<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/ac7\/95c\/b93\/ac795cb93a894b15a2642302d277dbef.jpg\"\/><\/p>\n<h4>\u041a\u043e\u0434 <\/h4>\n<p>  \u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u0447\u0435\u0440\u0435\u0434\u044c<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">namespace dispatch{     typedef std::function&lt;void ()&gt; function;     struct queue {         typedef long priority; \/\/ \u041d\u0430\u0448 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442. \u041f\u0443\u0441\u0442\u044c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e         const queue::priority queue_priority;           static std::shared_ptr&lt;queue&gt; main_queue() ; \/\/ \u041e\u0431 \u044d\u0442\u043e\u043c \u043d\u0438\u0436\u0435          virtual void async(function) const; \/\/ \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c          queue(queue::priority priority) : queue_priority(priority) {};     }; }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430 async <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u0432 thread pool:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    void queue::async(dispatch::function task) const {         thread_pool::shared_pool()-&gt;push_task_with_priority(task, this-&gt;queue_priority);     };<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0412\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0432 \u043d\u0430\u0448\u0435\u043c <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0443\u043b\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    struct queue_impl{         const queue::priority priority;         std::queue&lt;function&gt; tasks;         bool is_running;         queue_impl(queue::priority priority): priority(priority){};     };          struct thread_pool{         thread_pool();         static std::shared_ptr&lt;thread_pool&gt;& shared_pool(); \/\/ thread_pool         virtual ~thread_pool();                  bool stop;                  typedef std::shared_ptr&lt;queue_impl&gt; queue_ptr;                   void push_task_with_priority(const function&, queue::priority);\/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c         bool get_free_queue(queue_ptr*) const;          \/\/ \u0418\u0449\u0435\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441         void start_task_in_queue(const queue_ptr&);  \/\/ \u041e\u0442\u043c\u0435\u0447\u0430\u0435\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0443\u044e\u0441\u044f         void stop_task_in_queue(const queue_ptr&);  \/\/ \u0421\u043d\u0438\u043c\u0430\u0435\u043c \u043e\u0442\u043c\u0435\u0442\u043a\u0443                  std::mutex mutex; \/\/ \u041c\u044e\u0442\u0435\u043a\u0441 \u0434\u043b\u044f \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0443\u043b\u0430         std::map&lt;queue::priority, queue_ptr&gt; queues; \/\/ \u0417\u0434\u0435\u0441\u044c \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430\u043c                  std::mutex main_thread_mutex;         std::queue&lt;dispatch::function&gt; main_queue;                  std::condition_variable condition;         std::vector&lt;std::thread&gt; threads; \/\/ \u041c\u0430\u0441\u0441\u0438\u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438                  dispatch::function main_loop_need_update;         void add_worker(); \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a     };<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443. \u041d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0439\u0442\u0438 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043d\u0430\u0439\u0442\u0438 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">   bool thread_pool::get_free_queue(queue_ptr* out_queue) const {         \/\/  \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c \u043f\u043e \u0432\u0441\u0435\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044f\u043c \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c \u043e\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e        auto finded = std::find_if(queues.rbegin(), queues.rend(), [](const std::pair&lt;queue::priority, queue_ptr&gt;& iter){                                          return ! iter.second-&gt;is_running; \/\/ \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c                                      });                  bool is_free_queue_exist = (finded != queues.rend());         if (is_free_queue_exist)             *out_queue = finded-&gt;second;                  return  is_free_queue_exist;     }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    void thread_pool::push_task_with_priority(const function& task, queue::priority priority){         {             std::unique_lock&lt;std::mutex&gt; lock(mutex); \/\/ \u0417\u0430\u0445\u0432\u0430\u0442\u0432\u0430\u0435\u043c \u043c\u044e\u0442\u0435\u043a\u0441              \/\/ \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c. \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0435\u0442 - \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0435\u0435             auto queue = queues[priority];             if (!queue){                 queue = std::make_shared&lt;dispatch::queue_impl&gt;(priority);                 queues[priority] = queue;             }             queue-&gt;tasks.push(task);              \/\/ \u0415\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0438             unsigned max_number_of_threads = std::max&lt;unsigned&gt;(std::thread::hardware_concurrency(), 2);             unsigned number_of_threads_required = round(log(queues.size()) + 1);             while (threads.size() &lt; std::min&lt;unsigned&gt;(max_number_of_threads, number_of_threads_required)){                 add_worker();             }         }         condition.notify_one(); \/\/ \u041e\u043f\u043e\u0432\u0435\u0449\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a, \u0447\u0442\u043e \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c     }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u0442\u043c\u0435\u0447\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u043a\u0430\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u0443\u044e<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    void thread_pool::stop_task_in_queue(const queue_ptr& queue){         {             std::unique_lock&lt;std::mutex&gt; lock(mutex);            \/\/ \u041e\u0442\u043c\u0435\u0447\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u043a\u0430\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u0443\u044e. \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430 - \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u0435\u0435 \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439             queue-&gt;is_running = false;             if ( queue-&gt;tasks.size() ==0 ){                 queues.erase(queues.find(queue-&gt;queue_priority));             }         }         condition.notify_one(); \/\/ \u041e\u043f\u043e\u0432\u0435\u0449\u0430\u0435\u043c \u043f\u043e\u0442\u043e\u043a, \u0447\u0442\u043e \u043c\u044b \u043e\u0434\u043d\u0430 \u0438\u0437 \u0437\u0430\u0434\u0430\u0447 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u0430     }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0418, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0441\u0430\u043c \u043f\u043e\u0442\u043e\u043a:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    void thread_pool::add_worker(){         threads.push_back(std::thread([=]{                               dispatch::function task;                               thread_pool::queue_ptr queue;                               while(true){                                   {                                       std::unique_lock&lt;std::mutex&gt; lock(mutex); \/\/ \u041f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0437\u0430\u0445\u0432\u0430\u0442\u0438\u0442\u044c \u043c\u044e\u0442\u0435\u043a\u0441                                                                              while(!stop && !get_free_queue(&queue)) \/\/ \u0415\u0441\u043b\u0438 \u043d\u0435\u0442 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0445 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439                                           condition.wait(lock);                   \/\/ \u0422\u043e \u0436\u0434\u0435\u043c \u043e\u043f\u043e\u0432\u0435\u0449\u0435\u043d\u0438\u044f                                                                           if(stop) \/\/ \u0415\u0441\u043b\u0438 \u043f\u0443\u043b\u043b \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0431\u044b\u043b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d, \u0442\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c\u0441\u044f                                           return;                                        task = queue-&gt;tasks.front(); \/\/ \u0417\u0430\u0431\u0438\u0440\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438                                       queue-&gt;tasks.pop();                                         start_task_in_queue(queue); \/\/ \u041e\u0442\u043c\u0435\u0447\u0430\u0435\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043a\u0430\u043a \u0437\u0430\u043d\u044f\u0442\u0443\u044e                                   }                                   task(); \/\/ \u0418\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443                                   stop_task_in_queue(queue); \/\/ \u041e\u0442\u043c\u0435\u0447\u0430\u0435\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043a\u0430\u043a \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0443\u044e                               }                           }));     }<\/code><\/pre>\n<\/div>\n<\/div>\n<h4>Main Thread \u0438 Run Loop<\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/18b\/9d2\/32a\/18b9d232af48e845ccdc68606c692b6f.jpg\"\/><\/p>\n<p>  \u0412 \u0421++ \u043d\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u043d\u044f\u0442\u0438\u044f \u043a\u0430\u043a \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a. \u041d\u043e \u043d\u0430 \u044d\u0442\u043e\u0439 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0441\u0435 UI \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. UI \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430. \u0417\u043d\u0430\u0447\u0438\u0442, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0431\u043e \u0441\u0430\u043c\u0438\u043c \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u0442\u044c Run Loop, \u043b\u0438\u0431\u043e \u0432\u043a\u043b\u0438\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439. <\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0434\u043b\u044f \u00ab\u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430\u00bb:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Main Queue<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    struct main_queue : queue{         virtual void async(dispatch::function task) const override;         main_queue(): queue(0) {};     };      std::shared_ptr&lt;queue&gt; queue::main_queue(){         return std::static_pointer_cast&lt;dispatch::queue&gt;(std::make_shared&lt;dispatch::main_queue&gt;());     }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0410 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 async \u0431\u0443\u0434\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    void main_queue::async(dispatch::function task) const {         auto pool = thread_pool::shared_pool();         std::unique_lock&lt;std::mutex&gt; lock(pool-&gt;main_thread_mutex);         pool-&gt;main_queue.push(task);         if (pool-&gt;main_loop_need_update != nullptr)             pool-&gt;main_loop_need_update();     }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041d\u0443 \u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u0437 \u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u043e\u0434<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">    void process_main_loop() {         auto pool = thread_pool::shared_pool();         std::unique_lock&lt;std::mutex&gt; lock(pool-&gt;main_thread_mutex);         while (!pool-&gt;main_queue.empty()) {             auto task = pool-&gt;main_queue.front();             pool-&gt;main_queue.pop();             task();         }     }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/e8d\/eef\/ad3\/e8deefad3a0c113968d1e4812aead585.jpg\"\/><\/p>\n<h4>\u0422\u0435\u043f\u0435\u0440\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0432\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u0430: \u00ab\u041a\u0430\u043a?\u00bb \u0438 \u00ab\u0417\u0430\u0447\u0435\u043c?\u00bb<\/h4>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u00ab\u0417\u0430\u0447\u0435\u043c?\u00bb: C++ \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u0440\u043e\u0441\u0441-\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u041f\u041e. \u0412 \u0443\u0433\u043e\u0434\u0443 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043c\u043d\u043e\u0433\u0438\u0445 \u0443\u0434\u043e\u0431\u043d\u044b\u0445 \u0432\u0435\u0449\u0435\u0439 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f. GCD \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u044b\u0439 \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044f\u043c\u0438. <br \/>  \u041d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441 \u00ab\u041a\u0430\u043a?\u00bb \u043d\u0435\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430. \u0412\u043a\u043b\u0438\u043d\u0438\u0442\u044c\u0441\u044f \u0440\u0430\u043d\u043b\u0443\u043f \u043c\u043e\u0436\u043d\u043e \u043f\u043e \u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u041c\u043d\u043e\u0433\u0438\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043f\u0440\u0435\u0434\u0430\u0441\u0442\u043e\u0432\u043b\u044f\u044e\u0442 API \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 iOS \u0435\u0441\u0442\u044c \u00abperformSelectorOnMainThread:\u00bb. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0448\u044c \u0437\u0430\u0434\u0430\u0442\u044c \u043a\u043e\u043b\u043b\u0431\u044d\u043a \u0447\u0435\u0440\u0435\u0437 dispatch::set_main_loop_process_callback:  <\/p>\n<pre><code class=\"objectivec\">-(void)dispatchMainThread{     dispatch::process_main_loop(); } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{     dispatch::set_main_loop_process_callback([=]{         [self performSelectorOnMainThread:@selector(dispatchMainThread) withObject:nil waitUntilDone:NO];     });     return YES; }<\/code><\/pre>\n<p>  \u0415\u0441\u043b\u0438-\u0436\u0435 \u043c\u044b \u0441\u0430\u043c\u0438 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u043c \u043d\u0430\u0448 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0440\u0430\u043d\u043b\u0443\u043f, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043a\u043e\u0435:<\/p>\n<pre><code class=\"cpp\">    void main_loop(dispatch::function main_loop_function);      void main_loop(dispatch::function main_loop_function){         auto main_queue = queue::main_queue();         while (!thread_pool::shared_pool()-&gt;stop) {             main_queue-&gt;async(main_loop_function);             process_main_loop();         }     }<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/14a\/97f\/130\/14a97f13021826707f3d38d78d1571d1.jpg\"\/><\/p>\n<h4>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0442\u043e, \u0440\u0430\u0434\u0438 \u0447\u0435\u0433\u043e \u044d\u0442\u043e \u0438 \u0437\u0430\u0442\u0435\u0432\u0430\u043b\u043e\u0441\u044c:<\/h4>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c 6 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439 \u0438 \u0437\u0430\u043f\u0438\u0445\u043d\u0435\u043c \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043f\u043e 6 \u0437\u0430\u0434\u0430\u043d\u0438\u0439:  <\/p>\n<pre><code class=\"cpp\">   auto main_thread_id = std::this_thread::get_id();     for (unsigned task = 0; task &lt; 6; ++task)     for (unsigned priority = 0; priority &lt; 6; ++priority){         dispatch::queue(priority).async([=]{             assert(std::this_thread::get_id() != main_thread_id);             std::string task_string = std::to_string(task);             std::string palceholder(1+priority*5, ' ');             dispatch::queue::main_queue()-&gt;async([=]{                 assert(std::this_thread::get_id() == main_thread_id);                 std::cout &lt;&lt; palceholder &lt;&lt; task_string &lt;&lt; std::endl;             });         });     }<\/code><\/pre>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0443\u044e \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">                           0                           1                      0                 0                           2                      1                 1                           3                      2                 2                           4                      3                 3                           5                      4                 4            0                      5                 5            1       0  0            2       1  1            3       2  2            4       3  3            5       4  4       5  5 <\/code><\/pre>\n<p>  \u00ab\u0421\u0442\u043e\u043b\u0431\u0438\u043a\u00bb \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u044c. \u0427\u0435\u043c \u043f\u0440\u0430\u0432\u0435\u0435, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0438\u0439 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442 \u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u041b\u0438\u043d\u0438\u044f \u2014 \u044d\u0442\u043e \u043a\u043e\u043b\u043b\u0431\u044d\u043a\u0438 \u043d\u0430 \u00ab\u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a\u00bb.<\/div>\n<\/div>\n<p>  \u041d\u0443 \u0438 \u043a\u043e\u0434 \u0434\u043b\u044f iOS:<\/p>\n<pre><code class=\"cpp\">    for (int i = 0; i &lt; 20; ++i){         dispatch::queue(dispatch::QUEUE_PRIORITY::DEFAULT).async([=]{             NSAssert(![NSThread isMainThread], nil);             std::string first_string = std::to_string(i);             dispatch::queue::main_queue()-&gt;async([=]{                 NSAssert([NSThread isMainThread], nil);                 std::string second_string = std::to_string(i+1);                 std::cout &lt;&lt; first_string &lt;&lt; &quot; -&gt; &quot; &lt;&lt; second_string &lt;&lt; std::endl;                 [self.tableView reloadData]; \/\/ \u0414\u0435\u043b\u0430\u0435\u043c \u0447\u0442\u043e-\u0442\u043e \u0441 UI. \u0422\u043e, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0433\u043b\u0430\u0432\u043d\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430             });         });     }<\/code><\/pre>\n<h4>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0431\u0443\u0434\u0435\u0442. \u042d\u0442\u043e\u0442 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u043f\u0438\u0441\u0430\u043b\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441 \u0446\u0435\u043b\u044c\u044e \u043f\u043e\u0449\u0443\u043f\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0432 C++11. \u041a\u043e\u0434 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0447\u0443\u0442\u044c \u0431\u043e\u043b\u0435\u0435 200 \u0441\u0442\u0440\u043e\u043a \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u0445\u043e\u0440\u043e\u0448\u0435\u0433\u043e \u0421++ \u043a\u043e\u0434\u0430, <a href=\"https:\/\/github.com\/nut-code-monkey\/dispatch\">\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u043d\u0430 \u0433\u0438\u0442\u0445\u0430\u0431\u0435<\/a>. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u043b\u0441\u044f \u043d\u0430 clang++ 5.0, g++-4.8 \u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c 2012 Visual Studio. \u0422\u043e \u0435\u0441\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u044b \u0443\u0436\u0435 \u0432 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e\u0439 \u043c\u0435\u0440\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 C++11. <\/p>\n<p>  \u041f\u044b.\u0421\u044b. \u041f\u0440\u0438\u0437\u044b\u0432\u0430\u044f \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b \u044f \u043e\u0442\u043d\u044e\u0434\u044c \u043d\u0435 \u043f\u0440\u0438\u0437\u044b\u0432\u0430\u044e \u0438\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0431\u043e\u0435\u0432\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445. \u0425\u043e\u0442\u044f, \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u043a\u0430\u043a \u0435\u0449\u0435 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u0432\u043e \u0447\u0442\u043e-\u0442\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u043e\u0435?<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041d\u0443 \u0438 \u0435\u0449\u0435 \u043f\u0430\u0440\u0430 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f \u043d\u0435 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u043b \u043a\u0443\u0434\u0430 \u0432\u043f\u0438\u0445\u043d\u0443\u0442\u044c \u0432 \u0441\u0442\u0430\u0442\u044c\u0435<\/b><\/p>\n<div class=\"spoiler_text\"><img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/7b8\/b92\/d2e\/7b8b92d2ea0fbfcc3f59a13d0d88961a.jpg\"\/><br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/e0d\/a9e\/1fa\/e0da9e1fa8900088bbf4eaa30bad52e8.jpg\"\/><br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/07e\/ad7\/536\/07ead7536c6d4f44b3cb352e06dc8be5.jpg\"\/><\/div>\n<\/div>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/186200\/\"> http:\/\/habrahabr.ru\/post\/186200\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">\n<h4>\u0418\u041c\u0425\u041e (\u0418\u043c\u0435\u044e \u041c\u043d\u0435\u043d\u0438\u0435 \u0425\u0440\u0435\u043d \u041e\u0441\u043f\u043e\u0440\u0438\u0448\u044c)<\/h4>\n<p>  \u0421 \u043c\u043e\u0435\u0439 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u043e\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u0434\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u2014 \u044d\u0442\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0432. \u0412\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u2014 \u043e\u0447\u0435\u043d\u044c \u0443\u0432\u043b\u0435\u043a\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441. \u0418\u043d\u043e\u0433\u0434\u0430 \u043e\u043d \u0443\u0432\u043b\u0435\u043a\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0430, \u0440\u0430\u0434\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u0430\u043c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0438 \u0437\u0430\u0442\u0435\u0432\u0430\u043b\u0441\u044f. \u041f\u0440\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430 (\u043f\u043e\u0434 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u043c \u044f \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e) \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0438 \u0442\u0435\u0445\u043d\u0438\u043a.<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/15a\/1bf\/b69\/15a1bfb6986f94d613aeaab475a50e86.jpg\"\/>  <\/p>\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-186200","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/186200","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=186200"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/186200\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}