What's wrong with this simple multithreaded function that uses regex?
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(); } ...
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~
I don't see anything obviously wrong with that. My only question, is you say BOOST_HAS_THREADS is defined, are you defining it, or is Boost.Config defining it (if Boost.Config *isn't* setting it, then your project settings are wrong for multithreaded code)? Are you selecting the regex lib to link to manually or letting the auto-linking code do it's work (for minimum chances of bad things happening you should let the auto-linking code select the correct binary)? HTH, John.
2011/8/1 Andrew Yancy
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
You should use _beginthread or _beginthreadex to start C++ thread when using MSVC. CreateThread is too low level and does not do the per thread initialization required. If you read MSDN documentation it explain why CreateThread cannot be use with C++ program. Daniel Anderson
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
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Tue, 02 Aug 2011 19:07:44 +0200, Oliver Seiler
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. Just a remark: with gcc it is thread-safe
I'm guessing that is dependent on the version of gcc; I've hit this
problem with gcc on Linux, on various versions up to and including gcc
4.1.something. Do you know when specifically this was fixed in gcc?
On Wed, Aug 3, 2011 at 12:32 AM,
On Tue, 02 Aug 2011 19:07:44 +0200, Oliver Seiler
wrote: 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.
Just a remark: with gcc it is thread-safe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Thu, 04 Aug 2011 01:55:07 +0200, Oliver Seiler
I'm guessing that is dependent on the version of gcc; I've hit this problem with gcc on Linux, on various versions up to and including gcc 4.1.something. Do you know when specifically this was fixed in gcc?
Its a gcc feature, not something to be fixed ;) I found in http://gcc.gnu.org/gcc-4.0/changes.html, which describes new changes for gcc 4.0.4: "The compiler now uses the library interface specified by the C++ ABI for thread-safe initialization of function-scope static variables." Apparently the support is already in 4.0.4, earlier it might be there as well, but maybe in some unsatisfying way.
They lie :)
Seriously though, have hit this problem at various times, definitely
with more recent versions of gcc than 4.0.4 on Linux x86 32-bit and
64-bit; suppose I might need to chase this one down more thoroughly at
some point.
On Thu, Aug 4, 2011 at 2:39 AM,
On Thu, 04 Aug 2011 01:55:07 +0200, Oliver Seiler
wrote: I'm guessing that is dependent on the version of gcc; I've hit this problem with gcc on Linux, on various versions up to and including gcc 4.1.something. Do you know when specifically this was fixed in gcc?
Its a gcc feature, not something to be fixed ;) I found in http://gcc.gnu.org/gcc-4.0/changes.html, which describes new changes for gcc 4.0.4: "The compiler now uses the library interface specified by the C++ ABI for thread-safe initialization of function-scope static variables." Apparently the support is already in 4.0.4, earlier it might be there as well, but maybe in some unsatisfying way.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (5)
-
Andrew Yancy
-
Daniel Anderson
-
John Maddock
-
Oliver Seiler
-
Viatcheslav.Sysoltsev@h-d-gmbh.de