{"id":2182,"date":"2017-11-05T17:14:58","date_gmt":"2017-11-05T22:14:58","guid":{"rendered":"https:\/\/bitpost.com\/news\/?p=2182"},"modified":"2017-11-05T17:26:40","modified_gmt":"2017-11-05T22:26:40","slug":"c11-interactive-thread","status":"publish","type":"post","link":"https:\/\/bitpost.com\/news\/2017\/c11-interactive-thread\/","title":{"rendered":"C++11 interactive thread"},"content":{"rendered":"<p>This is a sweet spot of thread functionality for me at the moment, mixing boost and c++11, so I&#8217;m throwing it down here.\u00a0 It&#8217;s also in my Reusable code on git.<\/p>\n<pre>\/\/ --------------------------------------------------------------------\r\n\/\/ CONCISE EXAMPLE OF THREAD WITH EXTERNALLY-ACCESSIBLE STATUS AND DATA\r\n\/\/ --------------------------------------------------------------------\r\n\/\/ We create a vector, and create a thread to start stuffing it.\r\n\/\/ Externally, we can check the status of the job, and have mutex access to the data.\r\n\/\/ The atomic job stage is SO CHEAP to change and check, do it all day long as needed.\r\n\/\/ Initially, externally, we check the job stage.\r\n\/\/ Meanwhile, we do a bunch of intense work inside the mutex.\r\n\/\/ Then we do smaller work with frequent mutexing, allowing interruption.\r\n\/\/ Great patterns, use them brother!\r\n\/\/ Hells to the yeah.\r\n\/\/ --------------------------------------------------------------------\r\nstd::atomic&lt;int32_t&gt; job_stage(0);\r\nstd::unordered_set&lt;int&gt; data;\r\nboost::shared_mutex data_guard;\r\ndata.insert(-1);\r\nstd::thread t([&amp;job_stage,&amp;data,&amp;data_guard] {\r\n    \/\/ stage 1 = jam in data\r\n    job_stage = 1;\r\n    {\r\n        boost::upgrade_lock&lt;boost::shared_mutex&gt; lock(data_guard);\r\n        boost::upgrade_to_unique_lock&lt;boost::shared_mutex&gt; uniqueLock(lock);\r\n        for (int loop = 0; loop &lt; 2000; ++loop)\r\n        {\r\n            std::this_thread::sleep_for(1ms);\r\n            data.insert(loop);\r\n        }\r\n    }\r\n    \/\/ stage 2 = mutex-jam data, allowing intervention\r\n    job_stage = 2;\r\n    for (int loop = 3000000; loop &lt; 4000000 &amp;&amp; job_stage == 2; ++loop)\r\n    {\r\n        boost::upgrade_lock&lt;boost::shared_mutex&gt; lock(data_guard);\r\n        boost::upgrade_to_unique_lock&lt;boost::shared_mutex&gt; uniqueLock(lock);\r\n        data.insert(loop);\r\n    }\r\n    cout &lt;&lt; \"thread exiting...\" &lt;&lt; endl;\r\n});\r\n\r\ncout &lt;&lt; \"pre mutex job stage: \" &lt;&lt; job_stage &lt;&lt; endl;\r\n\r\nfor (int check = 0; check &lt; 5; ++check)\r\n{\r\n    std::this_thread::sleep_for(200ms);\r\n    \/\/ not sure why i was getting std::hex output...\r\n    cout &lt;&lt; \"check \" &lt;&lt; check &lt;&lt; \" job stage: \";\r\n    {\r\n        boost::upgrade_lock&lt;boost::shared_mutex&gt; lock(data_guard);\r\n        cout  &lt;&lt; dec &lt;&lt; job_stage &lt;&lt; \" data size \" &lt;&lt; data.size();\r\n    }\r\n    cout &lt;&lt; endl;\r\n}\r\n\r\n\/\/ We can trigger the thread to exit.\r\njob_stage = 3;\r\n\r\n\/\/ Let's see what happens if we don't join until after the thread is done.\r\nstd::this_thread::sleep_for(300ms);\r\ncout &lt;&lt; \"done sleeping\" &lt;&lt; endl;\r\n\r\n\/\/ NOW we will block to ensure the thread finishes.\r\ncout &lt;&lt; \"joining\" &lt;&lt; endl;\r\nt.join();\r\ncout &lt;&lt; \"all done\" &lt;&lt; endl;\r\n\/\/ --------------------------------------------------------------------<\/pre>\n<p>Output:<\/p>\n<p><code>pre mutex job stage: 1<br \/>\ncheck 0 job stage: 2 data size 2031<br \/>\ncheck 1 job stage: 2 data size 225848<br \/>\ncheck 2 job stage: 2 data size 456199<br \/>\ncheck 3 job stage: 2 data size 726576<br \/>\ncheck 4 job stage: 2 data size 936429<br \/>\nthread exiting...<br \/>\ncheck 5 job stage: 2 data size 1002001<br \/>\ndone sleeping<br \/>\njoining<br \/>\nall done<\/code><\/p>\n<pre><code><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is a sweet spot of thread functionality for me at the moment, mixing boost and c++11, so I&#8217;m throwing it down here.\u00a0 It&#8217;s also in my Reusable code on git. \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \/\/ CONCISE EXAMPLE OF THREAD WITH EXTERNALLY-ACCESSIBLE STATUS AND DATA \/\/ &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \/\/ We create a vector, and create a thread to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[22,19],"tags":[],"class_list":["post-2182","post","type-post","status-publish","format-standard","hentry","category-cpp","category-opensource"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9M11L-zc","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/comments?post=2182"}],"version-history":[{"count":4,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2182\/revisions"}],"predecessor-version":[{"id":2186,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/posts\/2182\/revisions\/2186"}],"wp:attachment":[{"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/media?parent=2182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/categories?post=2182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitpost.com\/news\/wp-json\/wp\/v2\/tags?post=2182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}