{"id":466,"date":"2010-02-01T22:32:17","date_gmt":"2010-02-01T20:32:17","guid":{"rendered":"http:\/\/mariusbancila.ro\/blog\/?p=466"},"modified":"2011-05-19T15:41:19","modified_gmt":"2011-05-19T13:41:19","slug":"helpers-for-multithreading-cpp","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/","title":{"rendered":"Helpers For Multithreading in C++"},"content":{"rendered":"<p>On of the most important challenges nowadays in programming is concurrency. If we don&#8217;t learn to write programs that are able to run on multiple cores the progress in hardware will be pointless. But when you run multiple threads for various processing you might face the situation when you have to write over and over again same or similar code for creating the threads, setting up the parameters for the threads, joining the threads, checking the result, cleaning-up, etc.<\/p>\n<p>In this post I will show how you can create some helpers in C++ to simplify this process. This is not going to be a full solution, neither a solution that fits all needs, but can be a start.<\/p>\n<p>What I would like to have is a helper class that will take care of:<\/p>\n<ul>\n<li>finding how many threads can run (considering each available core can run a thread)<\/li>\n<li>creating and starting the threads<\/li>\n<li>joining the threads<\/li>\n<li>checking the result of the threads execution<\/li>\n<li>cleaning up<\/li>\n<\/ul>\n<p>The class show bellow does just that.<\/p>\n<pre name=\"code\" class=\"cpp\">\r\n#include &lt;windows.h&gt;\r\n\r\nclass ThreadHelper\r\n{\r\n\tLPVOID* m_Params;\r\n\tint m_ThreadsNo;\r\n\r\nprivate:\r\n\tint GetProcessorsCount()\r\n\t{\r\n\t\tSYSTEM_INFO info;\r\n\t\t::GetSystemInfo(&amp;info);\r\n\t\treturn info.dwNumberOfProcessors;\r\n\t}\r\n\r\npublic:\r\n\tThreadHelper()\r\n\t{\r\n\t\tm_ThreadsNo = GetProcessorsCount();\r\n\r\n\t\tm_Params = new LPVOID[m_ThreadsNo];\r\n\t\tfor(int i = 0; i &lt; m_ThreadsNo; ++i)\r\n\t\t\tm_Params[i] = NULL;\r\n\t}\r\n\r\n\tThreadHelper(int threadsNo)\r\n\t{\r\n\t\tif(threadsNo &lt; 1)\r\n\t\t\tm_ThreadsNo = GetProcessorsCount();\r\n\t\telse\r\n\t\t\tm_ThreadsNo = threadsNo;\r\n\r\n\t\tm_Params = new LPVOID[m_ThreadsNo];\r\n\t\tfor(int i = 0; i &lt; m_ThreadsNo; ++i)\r\n\t\t\tm_Params[i] = NULL;\r\n\t}\r\n\r\n\t~ThreadHelper()\r\n\t{\r\n\t\tdelete [] m_Params;\r\n\t}\r\n\r\n\tint GetThreadsNo() const {return m_ThreadsNo;}\r\n\tbool SetThreadParams(int threadIndex, LPVOID lpData)\r\n\t{\r\n\t\tif(threadIndex &gt;= 0 && threadIndex &lt; m_ThreadsNo)\r\n\t\t{\r\n\t\t\tm_Params[threadIndex] = lpData;\r\n\t\t\treturn true;\r\n\t\t}\r\n\r\n\t\treturn false;\r\n\t}\r\n\r\n\tbool Run(LPTHREAD_START_ROUTINE threadProc, BOOL startImmediatelly, DWORD timeout = INFINITE)\r\n\t{\r\n\t\tbool success = false;\r\n\r\n\t\tHANDLE* hThreads = new HANDLE[m_ThreadsNo];\r\n\t\tDWORD* dwThreadIds = new DWORD[m_ThreadsNo];\r\n\r\n\t\tbool allThreadsOK = true;\r\n\r\n\t\t\/\/ create the threads\r\n\t\tfor(int i = 0; i &lt; m_ThreadsNo &amp;&amp; allThreadsOK; ++i)\r\n\t\t{\r\n\t\t\thThreads[i] = ::CreateThread(\r\n\t\t\t\tNULL,\r\n\t\t\t\t0,\r\n\t\t\t\tthreadProc,\r\n\t\t\t\tm_Params[i],\r\n\t\t\t\tstartImmediatelly ? 0 : CREATE_SUSPENDED,\r\n\t\t\t\t&amp;dwThreadIds[i]);\r\n\r\n\t\t\tif(hThreads[i] == NULL)\r\n\t\t\t{\r\n\t\t\t\tfor(int j = 0; j &lt; i; ++j)\r\n\t\t\t\t{\r\n\t\t\t\t\t::CloseHandle(hThreads[j]);\r\n\t\t\t\t}\r\n\r\n\t\t\t\tallThreadsOK = false;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif(allThreadsOK)\r\n\t\t{\r\n\t\t\t\/\/ start the threads if they were suspended first\r\n\t\t\tif(!startImmediatelly)\r\n\t\t\t{\r\n\t\t\t\tfor(int i = 0; i &lt; m_ThreadsNo; ++i)\r\n\t\t\t\t{\r\n\t\t\t\t\t::ResumeThread(hThreads[i]);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ wait for all threads\r\n\t\t\tDWORD joinret = ::WaitForMultipleObjects(\r\n\t\t\t\tm_ThreadsNo, \r\n\t\t\t\thThreads, \r\n\t\t\t\tTRUE, \r\n\t\t\t\ttimeout);\r\n\r\n\t\t\tif(joinret == WAIT_FAILED)\r\n\t\t\t{\r\n\r\n\t\t\t}\r\n\t\t\telse if(joinret = WAIT_TIMEOUT)\r\n\t\t\t{\r\n\r\n\t\t\t}\r\n\t\t\telse if(joinret &gt;= WAIT_OBJECT_0 &amp;&amp; joinret &lt; WAIT_OBJECT_0 + m_ThreadsNo)\r\n\t\t\t{\r\n\t\t\t\tsuccess = true;\r\n\t\t\t}\r\n\t\t\telse if(joinret &gt;= WAIT_ABANDONED_0 &amp;&amp; joinret &lt; WAIT_ABANDONED_0 + m_ThreadsNo)\r\n\t\t\t{\r\n\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ close the thread handles\r\n\t\t\tfor(int i = 0; i &lt; m_ThreadsNo; ++i)\r\n\t\t\t{\r\n\t\t\t\t::CloseHandle(hThreads[i]);\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tdelete [] hThreads;\r\n\t\tdelete [] dwThreadIds;\r\n\r\n\t\treturn success;\r\n\t}\r\n};\r\n<\/pre>\n<p>This helper class contains:<\/p>\n<ul>\n<li>one parameter-less constructor that identifies the number of available processors and sets the threads count equal to the processors count<\/li>\n<li>one constructor that takes the number of threads that should be created<\/li>\n<li>one method (SetThreadParams) for setting the parameters for each thread that will be created<\/li>\n<li>one method (Run) that creates and runs the thread, waits for them and checks the result of the execution<\/li>\n<\/ul>\n<p>As you can see the Run() method is simplistic. It does not handle for instance timed out or abandoned thread executions. Also it joins all threads, waiting until all of them finished execution. A more flexible method could wait only until the first thread finishes and then maybe closes the other threads. But as I said, this is a sample and not a complete solution.<\/p>\n<p>Having this helper set up, I will start several threads to find the prime numbers in a sequence and print them in the console. <\/p>\n<p>The following function computes whether a number is prime\/<\/p>\n<pre name=\"code\" class=\"cpp\">\r\n#include &lt;cmath&gt;\r\n\r\nbool IsPrime(int number)\r\n{\r\n\tconst int max = static_cast&lt; int &gt;(\r\n\t\tstd::sqrt(static_cast&lt; double &gt;(number))) + 1;\r\n\r\n\tfor (int i=2; i!=max; ++i)\r\n\t{\r\n\t\tif (number % i == 0) return false;\r\n\t}\r\n\r\n\treturn true;\r\n}\r\n<\/pre>\n<p>The thread procedure will run through a sub-sequence of a vector of integers and verify if each element is prime. I will use the following structure to pass the sequence bounds to the thread procedure:<\/p>\n<pre name=\"code\" class=\"cpp\">\r\nstruct vector_bounds\r\n{\r\n\tstd::vector&lt; int &gt;::const_iterator begin;\r\n\tstd::vector&lt; int &gt;::const_iterator end;\r\n};\r\n<\/pre>\n<p>The thread procedure could look like this:<\/p>\n<pre name=\"code\" class=\"cpp\">\r\nstatic CRITICAL_SECTION cs;\r\n\r\nDWORD WINAPI FindPrimes(LPVOID lpData)\r\n{\r\n\tvector_bounds* bounds = static_cast&lt; vector_bounds* &gt;(lpData);\r\n\tif(bounds == NULL)\r\n\t\treturn 1;\r\n\r\n\tfor(std::vector&lt; int &gt;::const_iterator cit = bounds-&gt;begin;\r\n\t\tcit != bounds-&gt;end; ++cit)\r\n\t{\r\n\t\tif(IsPrime(*cit))\r\n\t\t{\r\n\t\t\tEnterCriticalSection(&amp;cs);\r\n\r\n\t\t\tstd::cout &lt;&lt; *cit &lt;&lt; std::endl;\r\n\r\n\t\t\tLeaveCriticalSection(&amp;cs);\r\n\t\t}\r\n\t}\r\n\r\n\treturn 0;\r\n};\r\n<\/pre>\n<p>To print to the console a locking mechanism is necessary, otherwise prints from two different threads could collide. The critical section will be initialized before the threads are started.<\/p>\n<p>What remains to be done is generating a sequence of integers, setting up the parameters with the sequence bounds for each thread and run the threads using the helper.<\/p>\n<pre name=\"code\" class=\"cpp\">\r\nint main()\r\n{\r\n\t\/\/ generate some random numbers\r\n\tsrand((unsigned long)time(NULL));\r\n\tstd::vector&lt; int &gt; numbers;\r\n\tstd::generate_n(std::back_inserter(numbers), 1000, rand);\r\n\r\n\t\/\/ create the thread helper\r\n\tThreadHelper helper(4);\r\n\tint threads = helper.GetThreadsNo();\r\n\r\n\t\/\/ create the parameters for the threads\r\n\tstd::vector&lt; vector_bounds &gt; params;\r\n\tstd::vector&lt; int &gt;::const_iterator begin = numbers.begin();\r\n\tsize_t partitionsize = numbers.size()\/threads;\r\n\r\n\tfor(int i = 0; i &lt; threads; ++i)\r\n\t{\r\n\t\tvector_bounds bound;\r\n\t\tbound.begin = begin;\r\n\t\tbound.end = (i == threads - 1) ? numbers.end() : begin + partitionsize;\r\n\t\tparams.push_back(bound);\r\n\r\n\t\tbegin = bound.end;\r\n\t}\r\n\r\n\tfor(int i = 0; i &lt; threads; ++i)\r\n\t\thelper.SetThreadParams(i, &amp;params[i]);\r\n\r\n\t\/\/ run the threads\r\n\tInitializeCriticalSection(&amp;cs);\r\n\r\n\tstd::cout &lt;&lt; \"start running...\" &lt;&lt; std::endl;\r\n\r\n\tbool success = helper.Run(FindPrimes, FALSE);\r\n\r\n\tstd::cout &lt;&lt; \"finished \" &lt;&lt; (success? \"successfully\" : \"failed\") &lt;&lt; std::endl;\r\n\r\n\tDeleteCriticalSection(&amp;cs);\r\n\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>Having this threads helper class, what I need to do when running some processing in several threads is:<\/p>\n<ul>\n<li>setup thread parameters (if the case)<\/li>\n<li>write the thread procedure<\/li>\n<li>create a ThreadHelper object and initialize it<\/li>\n<li>run the threads and collect the results<\/li>\n<\/ul>\n<p>The helper class prevents writing same code over and over again and helps focusing on the most important tasks: writing the thread procedure. As I said earlier it is not a full solution, nor one that fits all scenarios, but you can develop it to suit your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On of the most important challenges nowadays in programming is concurrency. If we don&#8217;t learn to write programs that are able to run on multiple cores the progress in hardware will be pointless. But when you run multiple threads for various processing you might face the situation when you have to write over and over &#8230; <a title=\"Helpers For Multithreading in C++\" class=\"read-more\" href=\"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/\" aria-label=\"Read more about Helpers For Multithreading in C++\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7,42],"tags":[192],"class_list":["post-466","post","type-post","status-publish","format-standard","hentry","category-c","category-parallel-programming","tag-c-multithreads-threads-concurrency"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.3 - aioseo.com -->\n\t<meta name=\"description\" content=\"create helpers in C++ to simplify multi-threading code\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,helper,thread,threading,concurrency,c++ multithreads threads concurrency\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.3\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Helpers For Multithreading in C++\" \/>\n\t\t<meta property=\"og:description\" content=\"create helpers in C++ to simplify multi-threading code\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2010-02-01T20:32:17+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2011-05-19T13:41:19+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Helpers For Multithreading in C++\" \/>\n\t\t<meta name=\"twitter:description\" content=\"create helpers in C++ to simplify multi-threading code\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#article\",\"name\":\"Helpers For Multithreading in C++\",\"headline\":\"Helpers For Multithreading in C++\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"datePublished\":\"2010-02-01T22:32:17+02:00\",\"dateModified\":\"2011-05-19T15:41:19+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#webpage\"},\"articleSection\":\"C++, Parallel Programming, C++ multithreads threads concurrency\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#listItem\",\"name\":\"Helpers For Multithreading in C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#listItem\",\"position\":5,\"name\":\"Helpers For Multithreading in C++\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/\",\"name\":\"Helpers For Multithreading in C++\",\"description\":\"create helpers in C++ to simplify multi-threading code\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2010\\\/02\\\/01\\\/helpers-for-multithreading-cpp\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2010-02-01T22:32:17+02:00\",\"dateModified\":\"2011-05-19T15:41:19+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Helpers For Multithreading in C++","description":"create helpers in C++ to simplify multi-threading code","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/","robots":"max-image-preview:large","keywords":"c++,helper,thread,threading,concurrency,c++ multithreads threads concurrency","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#article","name":"Helpers For Multithreading in C++","headline":"Helpers For Multithreading in C++","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"datePublished":"2010-02-01T22:32:17+02:00","dateModified":"2011-05-19T15:41:19+02:00","inLanguage":"en-US","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#webpage"},"articleSection":"C++, Parallel Programming, C++ multithreads threads concurrency"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#listItem","name":"Helpers For Multithreading in C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#listItem","position":5,"name":"Helpers For Multithreading in C++","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/","name":"Helpers For Multithreading in C++","description":"create helpers in C++ to simplify multi-threading code","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2010-02-01T22:32:17+02:00","dateModified":"2011-05-19T15:41:19+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"Helpers For Multithreading in C++","og:description":"create helpers in C++ to simplify multi-threading code","og:url":"https:\/\/mariusbancila.ro\/blog\/2010\/02\/01\/helpers-for-multithreading-cpp\/","article:published_time":"2010-02-01T20:32:17+00:00","article:modified_time":"2011-05-19T13:41:19+00:00","twitter:card":"summary","twitter:title":"Helpers For Multithreading in C++","twitter:description":"create helpers in C++ to simplify multi-threading code"},"aioseo_meta_data":{"post_id":"466","title":"Helpers For Multithreading in C++","description":"create helpers in C++ to simplify multi-threading code","keywords":[{"label":"C++","value":"C++"},{"label":"helper","value":"helper"},{"label":"thread","value":"thread"},{"label":"threading","value":"threading"},{"label":"concurrency","value":"concurrency"}],"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"location":null,"local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:14:52","updated":"2025-12-12 07:31:10","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":1,"uagb_excerpt":"On of the most important challenges nowadays in programming is concurrency. If we don&#8217;t learn to write programs that are able to run on multiple cores the progress in hardware will be pointless. But when you run multiple threads for various processing you might face the situation when you have to write over and over&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/parallel-programming\/\" class=\"advgb-post-tax-term\">Parallel Programming<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">Parallel Programming<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/parallel-programming\/\" class=\"advgb-post-tax-term\">C++ multithreads threads concurrency<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++ multithreads threads concurrency<\/span>"]}},"comment_count":"1","relative_dates":{"created":"Posted 17 years ago","modified":"Updated 15 years ago"},"absolute_dates":{"created":"Posted on February 1, 2010","modified":"Updated on May 19, 2011"},"absolute_dates_time":{"created":"Posted on February 1, 2010 10:32 pm","modified":"Updated on May 19, 2011 3:41 pm"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-7w","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=466"}],"version-history":[{"count":6,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions"}],"predecessor-version":[{"id":872,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions\/872"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}