Boost logo

Boost Users :

Subject: Re: [Boost-users] What's wrong with this simple multithreaded function that uses regex?
From: Daniel Anderson (wonixen_at_[hidden])
Date: 2011-08-02 17:59:53


2011/8/2 Andrew Yancy <andrew732_at_[hidden]>

> >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_at_[hidden]
> 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


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