Re: [Boost-users] What's wrong with this simple multithreaded function that uses regex?

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.
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
Thanks for the replies John and Daniel. I've reinstalled boost 1.47 making sure that all the multithreaded options were built and am using _beginthreadex() now, but the exact same problem seems to occur where the code snippet below crashes if and only if boost::regex_match() is called. I am not defining BOOST_HAS_THREADS in my code, it is defined automatically although I'm not sure how exactly. Also, as far as I know I am not manually linking to the necessary libraries; all I do is tell MSVC to look in "C:\Program Files\boost\boost_1_47_0\stage\lib" for additional libraries so presumably it chooses whichever one it needs. There are no thread or boost-related warnings in the build. Any other ideas? I really appreciate the 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(); } ...

2011/8/2 Andrew Yancy
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.
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
Thanks for the replies John and Daniel. I've reinstalled boost 1.47 making sure that all the multithreaded options were built and am using _beginthreadex() now, but the exact same problem seems to occur where the code snippet below crashes if and only if boost::regex_match() is called. I am not defining BOOST_HAS_THREADS in my code, it is defined automatically although I'm not sure how exactly. Also, as far as I know I am not manually linking to the necessary libraries; all I do is tell MSVC to look in "C:\Program Files\boost\boost_1_47_0\stage\lib" for additional libraries so presumably it chooses whichever one it needs. There are no thread or boost-related warnings in the build.
Any other ideas? I really appreciate the 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
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 -- Pauvres touristes offrant aux cieux Leurs vie perdues, leurs tas de suif Ils appelaient tranquillité ce qui n'était qu'une noyade Richard Desjardins

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">To: >boost-users@lists.boost.org >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
participants (2)
-
Andrew Yancy
-
Daniel Anderson