Boost C++ Libraries: Ticket #13349: sregex not thread-safe when copied https://svn.boost.org/trac10/ticket/13349 <p> The following code does fail about every other execution: </p> <div class="wiki-code"><div class="code"><pre> <span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/xpressive/xpressive.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/thread.hpp&gt;</span><span class="cp"></span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> <span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">xpressive</span><span class="p">;</span> <span class="k">struct</span> <span class="n">MyRegexContainer</span> <span class="p">{</span> <span class="n">boost</span><span class="o">::</span><span class="n">xpressive</span><span class="o">::</span><span class="n">sregex</span> <span class="n">myPatterns</span><span class="p">[</span><span class="mi">20</span><span class="p">];</span> <span class="n">MyRegexContainer</span><span class="p">()</span> <span class="p">{</span> <span class="n">myPatterns</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">sregex</span><span class="o">::</span><span class="n">compile</span><span class="p">(</span><span class="s">&quot;[-]?[0-9]+</span><span class="se">\\</span><span class="s">.[0-9]{2}&quot;</span><span class="p">);</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">20</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="n">myPatterns</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">myPatterns</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">void</span> <span class="n">match</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">wert</span><span class="p">,</span> <span class="kt">int</span> <span class="n">index</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">regex_match</span><span class="p">(</span><span class="n">wert</span><span class="p">,</span> <span class="n">myPatterns</span><span class="p">[</span><span class="n">index</span><span class="p">])){</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Index &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">index</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; did not match value &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">wert</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="p">};</span> <span class="k">namespace</span> <span class="p">{</span> <span class="kt">void</span> <span class="n">testSREGEXMultithreadedTASK</span><span class="p">()</span> <span class="p">{</span> <span class="k">static</span> <span class="k">const</span> <span class="n">MyRegexContainer</span> <span class="n">a</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">10000</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span> <span class="n">a</span><span class="p">.</span><span class="n">match</span><span class="p">(</span><span class="s">&quot;33.00&quot;</span><span class="p">,</span> <span class="n">i</span><span class="o">%</span><span class="mi">20</span><span class="p">);</span> <span class="n">a</span><span class="p">.</span><span class="n">match</span><span class="p">(</span><span class="s">&quot;1.11&quot;</span><span class="p">,</span> <span class="n">i</span><span class="o">%</span><span class="mi">20</span><span class="p">);</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> <span class="kt">int</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span> <span class="cp">#ifdef BOOST_DISABLE_THREADS</span> <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;BOOST_DISABLE_THREADS == TRUE&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">wert</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span> <span class="cp">#endif</span> <span class="n">boost</span><span class="o">::</span><span class="n">thread_group</span> <span class="n">tgroup</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">100</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="p">{</span> <span class="n">tgroup</span><span class="p">.</span><span class="n">create_thread</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">bind</span><span class="p">(</span><span class="o">&amp;</span><span class="n">testSREGEXMultithreadedTASK</span><span class="p">));</span> <span class="p">}</span> <span class="n">tgroup</span><span class="p">.</span><span class="n">join_all</span><span class="p">();</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13349 Trac 1.4.3