开发者

c++ Boost Multithread Running Slow when two threads read file and tokenize the CSV File

I have two functions, it used to work for me, I did some changes in the codes but I don't know what happened. When I executed those functions as multi-threads, the CPU is 10-30%, and it is so slow. It just readfile by line then parse the CSV line using boost token.

        boost::thread OffLineUL(&AvaTTi::AvaCollectTTiAdvance::UeAndCellParamParseUL,c,tracefilenameUL.c_str(),NumOfLines,GHOSTFILTER,"","/",OffLineMode,OPENEXCELAUTO);
        boost::thread OffLineDL(&AvaTTi::AvaCollectTTiAdvance::UeAndCellParamParseDL,c,tracefilenameDL.c_str(), NumOfLines,"","/",OffLineMode,OPENEXCELAUTO);
        OffLineDL.join();
        OffLineUL.join();


    int AvaCollectTTiAdvance::UeAndCellParamParseDL(const char *inname, int NumOfRecords, const char *UserDir, const char* CurrentDir, int OffLineMode, int OPENEXCELAUTO)
    {
       typedef boost::tokenizer <boost::escaped_list_separator<char> > my_tokenizer;
       vector <string> mystr;


      std::ifstream infile(TTiAsciiTraceOutputUserDir.str(),std::ios::in);
      while (ge开发者_如何学编程tline(infile, line)  && lineCount <= NumOfRecords)

      for (my_tokenizer::iterator it(tok.begin()), end(tok.end()); it != end; ++it)
      { 
        mystr.push_back(*it);
      }
      ....................
      ....................

Can anyone please help? I am running out ideas. Thanks.


If you have shared resources amongst your threads, such as the vector mystr then you need to use thread synchronization mechanisms to access these resources, otherwise your vector will get corrupted.

This is true for any variable that will be accessed from multiple threads at once, if they are being written to by at least one thread.

I cannot say what the problem is as you are not providing enough data, I cannot make out from your code what variables are local, and which one's aren't and which variables are only used by one or by multiple threads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜