Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多线程应用程序中使用stringstream的分段错误(内核转储)_C++_Multithreading_C++11_Stringstream_Segmentation Fault - Fatal编程技术网

C++ 多线程应用程序中使用stringstream的分段错误(内核转储)

C++ 多线程应用程序中使用stringstream的分段错误(内核转储),c++,multithreading,c++11,stringstream,segmentation-fault,C++,Multithreading,C++11,Stringstream,Segmentation Fault,我正在尝试做简单的线程安全记录器,它将消息打印到控制台 // Test function for check logger. It is work void test(double& diff) { std::vector<double> result; for( int counter = 0; counter < 100000; ++counter) { result.push_back(clock()); std::string te

我正在尝试做简单的线程安全记录器,它将消息打印到控制台

// Test function for check logger. It is work
void test(double& diff)
{
  std::vector<double> result;
  for( int counter = 0; counter < 100000; ++counter)
  {
    result.push_back(clock());

    std::string text = "counter = ";
    text.append(std::to_string(counter));
    LOG_MESSAGE(text); //<-- Correct log..
  }
  diff = clock() - result.front();

}

int main(int argc, char** argv)
{
  double time2;
  double time1;
  std::vector<double> timerResult;
  std::vector<std::thread> threadVector;

  time1 = clock();
  for(int i = 0; i < 5; ++i) //<-- Create 5 thread of test function
  {
    timerResult.push_back(0);
    threadVector.push_back(std::thread(test, std::ref(timerResult[i])));
  }
  for(std::thread& t : threadVector)
    t.join();
  time2 = clock(); //<-- Threads is finished

  double res = 0;
  for(double tRes : timerResult)
     res += tRes;
  res = res / static_cast<double>(CLOCKS_PER_SEC);

  std::string message; //<-- Generate final message
  message.append("Timer: ")
      .append(std::to_string((time2 - time1) / (double)CLOCKS_PER_SEC))
      .append(" - thread timer: ")
      .append(std::to_string(res));

  LOG_MESSAGE(message); //<-- Crash inside!!    

  return 0;
}
//检查记录器的测试功能。这是工作
空隙试验(双重和差异)
{
std::向量结果;
用于(整数计数器=0;计数器<100000;++计数器)
{
结果:向后推(时钟());
std::string text=“counter=”;
append(std::to_字符串(计数器));
日志消息(文本);//问题已解决。
正如评论中所说,行
threadVector.push_back(std::thread(test,std::ref(timerResult[i]));
是不正确的,因为
timerResult
中的内存重新分配是在调用
push_back
5次之后完成的,并且
ref(timerResult[i])
传递引用是不正确的

正确代码:

    int main(int argc, char** argv)
    {
      double time2;
      double time1;
      std::vector<double> timerResult (5); //<-- Create filling of vector
      std::vector<std::thread> threadVector;

      time1 = clock();
      for(int i = 0; i < 5; ++i)
      {
        //timerResult.push_back(0); //<-- incorrect filling of vector
        threadVector.push_back(std::thread(test, std::ref(timerResult[i])));
      }
      for(std::thread& t : threadVector)
        t.join();
      time2 = clock();
      ...
    }
int main(int argc,char**argv)
{
双时间2;
双时间1;

标准::向量时间结果(5);//供参考文档注意,
localtime
可能不是线程安全的。如果将调用移动到锁内的
generateString
,会发生什么情况?分段错误不是突然出现的。您是否在调试器中检查堆栈跟踪,它确切来自您的代码?检查向量?记住向量将完全重新分配如果它的存储量增加,那么线程可能指向垃圾。使用reserve预先调整向量的大小可能会有所帮助。@RichardCriten我试图将generateString移到锁内。没有任何更改。@user0042是的,我检查stacktrace。调试器在main()中的LOG_消息中输入函数。正确生成度量值并落在stringstream析构函数上。
    int main(int argc, char** argv)
    {
      double time2;
      double time1;
      std::vector<double> timerResult (5); //<-- Create filling of vector
      std::vector<std::thread> threadVector;

      time1 = clock();
      for(int i = 0; i < 5; ++i)
      {
        //timerResult.push_back(0); //<-- incorrect filling of vector
        threadVector.push_back(std::thread(test, std::ref(timerResult[i])));
      }
      for(std::thread& t : threadVector)
        t.join();
      time2 = clock();
      ...
    }