{"id":7752,"date":"2018-05-05T09:27:23","date_gmt":"2018-05-05T09:27:23","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=7752"},"modified":"2018-05-05T13:20:13","modified_gmt":"2018-05-05T13:20:13","slug":"c-threads","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=7752","title":{"rendered":"C++ Threads"},"content":{"rendered":"<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\r\n#include &lt;thread&gt;\r\n#include &lt;mutex&gt;\r\n#include &lt;atomic&gt;\r\n\r\nstd::thread    t(thread_function);\r\n\r\nstd::thread::hardware_concurrency()\r\nstd::thread::get_id()\r\nstd::this_thread::get_id();\r\n\r\nt.join();\r\nt.detach();\r\nt.joinable();\r\nt.get_id();\r\n\r\nstatic std::mutex               m;\r\nstd::lock_guard&lt;std::mutex&gt;     guard(m);\r\nstd::unique_lock&lt;std::mutex&gt;    l(m);\r\nstd::atomic&lt;int&gt;                result(0);\r\nstd::condition_variable         condvar;\r\n\r\nm.lock();\r\nm.unlock();\r\ncondvar.wait(...);\r\ncondvar.notify_one();\r\n\r\n<\/pre>\n<ul>\n<li>Function Pointer<\/li>\n<li>Function Objects<\/li>\n<li>Lambda functions<\/li>\n<\/ul>\n<p><a href=\"https:\/\/solarianprogrammer.com\/2011\/12\/16\/cpp-11-thread-tutorial\/\">C++11 multithreading tutorial<\/a><br \/>\n<a href=\"https:\/\/solarianprogrammer.com\/2012\/02\/27\/cpp-11-thread-tutorial-part-2\/\">C++11 multithreading tutorial &#8211; part 2<\/a><br \/>\n<a href=\"http:\/\/thispointer.com\/c-11-multithreading-part-1-three-different-ways-to-create-threads\/\">C++11 Multithreading \u2013 Part 1 : Three Different ways to Create Threads<\/a><br \/>\n<a href=\"http:\/\/www.bogotobogo.com\/cplusplus\/C11\/1_C11_creating_thread.php\">C++11\/C++14 Thread 1. Creating Threads<\/a><\/p>\n<p><a href=\"https:\/\/baptiste-wicht.com\/categories\/c%2B%2B11-concurrency-tutorial.html\">Posts about C++11 Concurrency Tutorial<\/a><br \/>\n<a href=\"https:\/\/baptiste-wicht.com\/posts\/2012\/03\/cpp11-concurrency-part1-start-threads.html\">C++11 Concurrency &#8211; Part 1 : Start Threads<\/a><br \/>\n<a href=\"https:\/\/baptiste-wicht.com\/posts\/2012\/03\/cp11-concurrency-tutorial-part-2-protect-shared-data.html\">C++11 Concurrency Tutorial &#8211; Part 2 : Protect shared data<\/a><br \/>\n<a href=\"https:\/\/baptiste-wicht.com\/posts\/2012\/04\/c11-concurrency-tutorial-advanced-locking-and-condition-variables.html\">C++11 Concurrency Tutorial &#8211; Part 3: Advanced locking and condition variables<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>#include &lt;thread&gt; #include &lt;mutex&gt; #include &lt;atomic&gt; std::thread t(thread_function); std::thread::hardware_concurrency() std::thread::get_id() std::this_thread::get_id(); t.join(); t.detach(); t.joinable(); t.get_id(); static std::mutex m; std::lock_guard&lt;std::mutex&gt; guard(m); std::unique_lock&lt;std::mutex&gt; l(m); std::atomic&lt;int&gt; result(0); std::condition_variable condvar; m.lock(); m.unlock(); condvar.wait(&#8230;); condvar.notify_one(); Function Pointer Function Objects Lambda functions C++11 multithreading tutorial C++11 multithreading tutorial &#8211; part 2 C++11 Multithreading \u2013 Part 1 : Three Different ways to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7752","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/7752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7752"}],"version-history":[{"count":9,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/7752\/revisions"}],"predecessor-version":[{"id":7765,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/7752\/revisions\/7765"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}