Boost logo

Boost Users :

Subject: Re: [Boost-users] What's wrong with this simple multithreaded function that uses regex?
From: Andrew Yancy (andrew732_at_[hidden])
Date: 2011-08-02 23:31:55


Thanks Daniel and Oliver, I appreciate it.  The static expression is exactly what the problem was.  Here's a discussion in case anyone else ever comes across the same problem with the boost.regex example code that uses a static regex:

http://stackoverflow.com/questions/4590626/c-static-variables-in-multithreaded-program

>From:
>"Oliver Seiler" <oseiler_at_[hidden]>
>To:
>boost-users_at_[hidden]
>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.

>here few suggestions:
 - What if you put the regex automatic instead of static ?     often objects of classes a thread safe when different object are use (they do not share static data), but the  same object cannot be used within two thread  without some ways to serialize access which must be done by the user of the object. - if you keep the regex static within the function, at least make the construction thread safe. 

Daniel Anderson



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