What's wrong with this simple Boost multithreaded function?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: What's wrong with this simple Boost multithreaded function?

  1. #1
    Join Date
    Jul 2011
    Posts
    15

    What's wrong with this simple Boost multithreaded function?

    The code snippet below is the core of a function mythreadfunc() that is run concurrently for several threads using CreateThread() and WaitForMultipleObjects(). Everything seems to work fine except actually using boost::regex_match(). For example, if that line is changed to if(true), the entire multithreaded function works perfectly. Similarly, if mythreadfunc() as shown below is run "normally" 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 "access violation."

    This leads me to believe that boost::regex is not thread safe, 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! Thanks for any ideas or help~

    Code:
    	
    static const boost::regex expression("^[0-9]+");
    ifstream myfile(x);
    string line;
    if(myfile.is_open())
    {
    	while(myfile.good())
    	{
    		getline(myfile, line);
    		if(boost::regex_match(line, expression))
    		{
    			//do stuff
    		}
    }
    myfile.close();
    Last edited by andrew732; August 1st, 2011 at 12:51 AM.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: What's wrong with this simple Boost multithreaded function?

    Well, I'm not sure 'ifstream' is thread safe.

    Viggy

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: What's wrong with this simple Boost multithreaded function?

    Quote Originally Posted by andrew732 View Post
    The code snippet below is the core of a function mythreadfunc() that is run concurrently for several threads
    In general, it's better not to read/write to disk from several threads. Hard disk access is a bottleneck on normal pc's and reading several files at the same time can be much slower than reading them sequentially.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center