Boost logo

Boost Users :

Subject: Re: [Boost-users] What's wrong with this simple multithreaded function that uses regex?
From: Oliver Seiler (oseiler_at_[hidden])
Date: 2011-08-02 13:07:44


I've generally found that using function-level static with multiple
threads is a Bad Idea (including with Boost.Threads) as the object
won't get initialized until the function is run for the "first" time,
but this initialization isn't generally thread-safe. Consider moving
the static regex out of the function (e.g., into an anonymous
namespace) so that it is initialized at global static initialization
time.

On Sun, Jul 31, 2011 at 11:03 PM, Andrew Yancy <andrew732_at_[hidden]> wrote:
> The code snippet below is the core of a function mythreadfunc() that is
> run concurrently for several threads using the Win32 MSVC functions CreateThread() and WaitForMultipleObjects().  Everything seems to work fine except for actually using boost::regex_match().  For example, if that line below is changed to if(true), the entire multithreaded function works perfectly.  Similarly,  if mythreadfunc() as shown below is run without multiple threads, it works perfectly.  But if mythreadfunc() as shown below is run with multiple threads, the entire program crashes with return value 0xc0000005, which is some kind of memory access violation.
>
> This leads me to believe that boost::regex does not play well with MSVC threads, but according to the Boost site, there aren't any bugs related to this issue for Boost 1.45.  As far as I can tell I'm using Boost in thread safe mode (BOOST_HAS_THREADS is defined).  I'm probably doing something
> really dumb related to MSVC threads or related to the way I configured Boost, but I'm not sure what it is!  I could switch to using Boost threads, but I would like to minimize the changes to the code.  Thanks very much for any help~
>
>
>
> ...
> static const boost::regex expression("^[0-9]+");
> ifstream myfile(x); //x is a different file name for every thread
> string line;
> if(myfile.is_open())
> {
>   while(myfile.good())
>   {
>      getline(myfile, line);
>      if(boost::regex_match(line, expression))
>      {
>         //do stuff
>      }
>   }
>   myfile.close();
> }
> ...
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net