c++当两个线程读取文件并标记 CSV 文件时,提升多线程运行缓慢
我有两个函数,它曾经对我有用,我对代码做了一些更改,但我不知道发生了什么。当我以多线程方式执行这些函数时,CPU 为 10-30%,而且非常慢。它只是逐行读取文件,然后使用 boost 令牌解析 CSV 行。
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 (getline(infile, line) && lineCount <= NumOfRecords)
for (my_tokenizer::iterator it(tok.begin()), end(tok.end()); it != end; ++it)
{
mystr.push_back(*it);
}
....................
....................
有人可以帮忙吗?我的想法已经用完了。谢谢。
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 (getline(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的线程之间共享资源,例如向量 mystr,那么您需要使用线程同步机制来访问这些资源,否则您的向量将被损坏。
对于将从多个线程同时访问的任何变量来说都是如此,如果它们至少被一个线程写入的话。
我无法说出问题是什么,因为您没有提供足够的数据,我无法从您的代码中看出哪些变量是本地变量,哪些变量不是本地变量以及哪些变量仅由一个或多个线程使用。
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.