{"id":3483,"date":"2011-02-03T17:50:00","date_gmt":"2011-02-03T17:50:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2011\/02\/03\/concurrent-containers\/"},"modified":"2019-02-18T18:45:15","modified_gmt":"2019-02-18T18:45:15","slug":"concurrent-containers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/concurrent-containers\/","title":{"rendered":"Concurrent Containers"},"content":{"rendered":"<p>A recurrent question (well, one of the many) is about STL containers and whether they are thread safe.<\/p>\n<p>Taking Stephan&rsquo;s words here, the reality is that they aren&rsquo;t, not as a bug but as a feature: having every member function of every STL container acquiring an internal lock would annihilate performance. As a general purpose, highly reusable library, it wouldn&rsquo;t actually provide correctness either: the correct level to place locks is determined by what the program is doing. In that sense, individual member functions don&rsquo;t tend to be such correct level. <\/p>\n<p><span style=\"color: #ccb400\"><span style=\"font-family: Courier New\">\/\/ This article tells more about Thread Safety in the <br \/>\/\/ <\/span><span style=\"font-family: Courier New\">Standard C++ Library: <\/span><\/span><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/c9ceah3b.aspx\" title=\"http:\/\/msdn.microsoft.com\/en-us\/library\/c9ceah3b.aspx\"><span style=\"font-family: Courier New;color: #ccb400\">http:\/\/msdn.microsoft.com\/en-us\/library\/c9ceah3b.aspx<\/span><\/a><\/p>\n<p>Yet if performance were not an issue, for instance because it&rsquo;s not expected a high volume of write accesses to a container instance, but you just want to just make sure that simultaneous writes won&rsquo;t leave the container in an undetermined state, you may get among other alternatives the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd504906.aspx\">parallel containers<\/a> that come with the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd492418.aspx\">Parallel Patterns Library (PPL)<\/a>.<\/p>\n<p>They are basically <span style=\"font-family: Courier New\">concurrent_vector<\/span> and <span style=\"font-family: Courier New\">concurrent_queue<\/span>, so in case you missed concurrent versions of <a href=\"https:\/\/channel9.msdn.com\/shows\/Going+Deep\/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-2-of-n\/\">associative containers<\/a> like <span style=\"font-family: Courier New\">map<\/span>, <span style=\"font-family: Courier New\">multimap<\/span> or <span style=\"font-family: Courier New\">hashmap<\/span>, you may find sample implementations in <a href=\"http:\/\/code.msdn.microsoft.com\/concrtextras\" title=\"http:\/\/code.msdn.microsoft.com\/concrtextras\">http:\/\/code.msdn.microsoft.com\/concrtextras<\/a>.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A recurrent question (well, one of the many) is about STL containers and whether they are thread safe. Taking Stephan&rsquo;s words here, the reality is that they aren&rsquo;t, not as a bug but as a feature: having every member function of every STL container acquiring an internal lock would annihilate performance. As a general purpose, [&hellip;]<\/p>\n","protected":false},"author":293,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,80],"class_list":["post-3483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-container","tag-parallelism"],"acf":[],"blog_post_summary":"<p>A recurrent question (well, one of the many) is about STL containers and whether they are thread safe. Taking Stephan&rsquo;s words here, the reality is that they aren&rsquo;t, not as a bug but as a feature: having every member function of every STL container acquiring an internal lock would annihilate performance. As a general purpose, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/293"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=3483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3483\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=3483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=3483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=3483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}