c++ Boost 多线程 CPU 和内存使用率缓慢且高 - 需要帮助

标签 c++ multithreading boost

我正在使用 boost 库创建带互斥量的多线程。我基于谷歌上的一些例子。 multitrheads 工作正常,但 CPU 是 100%,任务管理器的内存使用是 300K+。我的两个多线程函数都执行非常高的数据转换和分析。我不确定我是否错过了什么。这是我的多线程的一部分:

wait(1); 
mutex.lock();
boost::thread t1(&YExcel::BasicExcelCell::TTiTraceParserConv,c,TTiAsciiTraceOutputDL.GetBuffer(0));
mutex.unlock();
wait(2); 
mutex.lock();
boost::thread t2(&YExcel::BasicExcelCell::TTiTraceParserConv,c,TTiAsciiTraceOutputUL.GetBuffer(0));
mutex.unlock();
t1.join();
t2.join();
wait(2); 

wait(1); 
mutex.lock();
boost::thread t5(&YExcel::BasicExcelCell::UeAndCellParamParseUL,c,TTiAsciiTraceOutputUL.GetBuffer(0), NumOfLinesUL,GHOSTFILTER);
mutex.unlock();
wait(2); 
mutex.lock();
boost::thread t6(&YExcel::BasicExcelCell::UeAndCellParamParseDL,c,TTiAsciiTraceOutputDL.GetBuffer(0), NumOfLinesDL);
mutex.unlock();
t5.join();
t6.join();

我需要在这些函数中做些什么吗?提前致谢。我使用 Dell Optilex 745 Pentium D 进行了测试。我不确定它是否支持多线程。我打印了 hard_concurrent,它显示在 1 上。

函数之一——总结

int BasicExcelCell::UeAndCellParamParseDL(char *inname, int NumOfRecords)
{
typedef boost::tokenizer <boost::escaped_list_separator<char> > my_tokenizer;   
....
....
CString TTiAsciiTraceOutput(inname);
TTiAsciiTraceOutput.Replace(".dat","_raw.txt");

ifstream infile(TTiAsciiTraceOutput.GetBuffer(0));
if (!infile)
{
    cout << "Couldn't open file " << TTiAsciiTraceOutput.GetBuffer(0) << " for reading." << endl;
    return EXIT_FAILURE;
}
cout << "\nOpened file " << TTiAsciiTraceOutput.GetBuffer(0) << " for reading." << endl << endl;

int lineCount=0;
getline(infile, line);
if (NumOfRecords < 0)
{
    cout<<"Number Of Lines to be parsed is not specified!"<<endl;
    return 0;
}
while (getline(infile, line) && lineCount <= NumOfRecords)
{       
    lineCount++;
    int FoundCrntiEmpty=0;

    my_tokenizer tok(line);
    int i = 0;

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

    int tokcount=0;
    int SingleTx=0;
    int TxDiv=0;
    int tokCountFlag=0;
    double MetricCalTemp=0.0;

    for (vector < string >::iterator mit(mystr.begin()); mit != mystr.end(); mit++)
    {
        for (vector < string >::iterator _mit(mystr.begin()); _mit != mystr.end(); _mit++)
        {
            tokCountFlag++;
            if (tokCountFlag==60)
                break;//not need to goto the whole iterator

            switch (tokCountFlag)
            {
                case 29: 
                     <<*_mit<<endl; 
                        if (*_mit=="0")
                            FoundCrntiEmpty=1;
                    break;

                case 58:
                    //cout<<"Single or Tx " <<*_mit<<endl;

                    if (*_mit == "0")
                        SingleTx=1;
                    else if (*_mit == "1")
                        TxDiv=1;

                    break;
            }
        } 
       }
 }

第一个线程上的另一个短函数:

void BasicExcelCell::TTiTraceParserConv(char *p_resFile)
{

char ttiTraceParser[512];
char ttiTraceConfig[512];
char cwd[512];

size_t size;

char OrigDLFile[512];

GetModuleFileName(NULL, cwd, 512);
char * CollectTTiTraceAdvance_exe;
CollectTTiTraceAdvance_exe = strstr (cwd,"CollectTTiTraceAdvance.exe");
strncpy (CollectTTiTraceAdvance_exe,"",26);

CString TTiAsciiTraceOutput(p_resFile);
CString ParserExe, TraceConfig;

ParserExe.Format(_T("%s\\BinaryFileParser\\tti_trace_parser_wmp.exe "),cwd);

sprintf(OrigDLFile,"%s",TTiAsciiTraceOutput.GetBuffer(0));
TTiAsciiTraceOutput.Replace(".dat","_raw.txt");
TraceConfig.Format(_T(" %s\\%s %s\\%s"),cwd,OrigDLFile,cwd,TTiAsciiTraceOutput);

cout<<"\n\n****Prepare to generate RawFile!!!"<<endl;
ShellAndWait(ParserExe.GetBuffer(0),TraceConfig.GetBuffer(0),"WAIT",240,1);//4 minutes

ParserExe.ReleaseBuffer(0);
TraceConfig.ReleaseBuffer(0);
}

最佳答案

这对我来说不对:

mutex.lock();
boost::thread t6(...);
mutex.unlock();

这只会保护线程的创建不会与其他任何东西并发运行,它不会影响该线程执行的操作。您需要从线程函数中锁定互斥量以获得任何保护。

此外,正如评论中指出的那样,wait 是可疑的,不需要。

最后,您应该(在我看来)永远不要直接使用 mutex.lock/mutex.unlock,因为它容易出错,尤其是存在异常的情况。使用 Boost 提供的 scoped_lock 等 RAII 工具。

要获得更多信息,您必须向我们展示这些线程函​​数实际在做什么。

关于c++ Boost 多线程 CPU 和内存使用率缓慢且高 - 需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559894/

相关文章:

c++ - 警告 : 'auto' type specifier is a C++11 extension [-Wc++11-extensions]

c++ - 如何将多个包裹一起寄送?

c++ - 具有 Python GUI 和 C++ 后台模块的混合应用程序

c++ - 将 CGAL 与四元数结合起来合乎逻辑吗

数据结构的 C++ 序列化

c++ - 同一台机器上的 libstdc++ 64 位和 32 位版本

c++ - 如何直接修改va_list结构成员?

java - 如何在Java代码中启动一组线程

linux - 移除 USB 键盘时出现“scheduling whileatomic”错误,如何读取错误消息?

multithreading - 将 Haskell 线程与内核线程进行比较——我的基准测试可行吗?