【问题标题】:Thread join cause a segmentation fault when another thread is running当另一个线程正在运行时,线程连接会导致分段错误
【发布时间】:2016-04-23 09:52:06
【问题描述】:

我有这个方法:

void Process::launch()
{
  std::thread readThread(&Process::readInfo, this);
  std::thread writeThread(&Process::writeInfo, this);

  readThread.join();
  this->_finished = true;
  writeThread.join();

  delete this->_namedPipeWrite;
  delete this->_namedPipeRead;
  std::cout << "No segmentation fault here" << std::endl;
  std::cout << "Segfault here" << std::endl;
  delete this->_threadPool;
  std::cout << "Never arrives here" << std::endl;
  _Exit(0);
}

当我尝试delete this-&gt;_threadPool时出现哪个段错误

threadPool 的解构器这样做:

 {
    std::unique_lock<std::mutex> lock(this->_mutex);

    this->_shutDown = true;
  }

  this->_cond.notify_all();

  for(std::thread &thread : this->_threadList)
  {
    thread.join();
  }

  this->_threadList.empty();
  this->_finished = true;
  delete this->_functionMutexes;

段错误发生在join()处。

我这样声明我的线程池:

  this->_finished = false;
  this->_shutDown = false;
  this->_functionMutexes = new std::vector<std::mutex>(numberOfThreads);
  for(int i = 0; i < numberOfThreads; ++i)
  {
    this->_threadList.emplace_back(std::thread(&ThreadPool::task, this));
  }

这是 valgrind:

==28174== Process terminating with default action of signal 11 (SIGSEGV)
==28174==  Access not within mapped region at address 0x99CD9D0
==28174==    at 0x4E3D600: pthread_join (pthread_join.c:45)
==28174==    by 0x540EE46: std::thread::join() (in /usr/lib64/libstdc++.so.6.0.21)
==28174==    by 0x4063F0: ThreadPool<int>::shutDown() (ThreadPool.cpp:52)
==28174==    by 0x406299: ThreadPool<int>::~ThreadPool() (ThreadPool.cpp:21)
==28174==    by 0x410B62: Process::launch() (Process.cpp:56)
==28174==    by 0x410873: Process::Process(int) (Process.cpp:24)
==28174==    by 0x40CC87: ProcessManager::addOrder(std::__cxx11::list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int) (in /home/danilo_d/Epitech-Projects/Semestre4/cpp_plazza/plazza)
==28174==    by 0x405934: main (in /home/danilo_d/Epitech-Projects/Semestre4/cpp_plazza/plazza)
==28174==  If you believe this happened as a result of a stack
==28174==  overflow in your program's main thread (unlikely but
==28174==  possible), you can try to increase the size of the
==28174==  main thread stack using the --main-stacksize= flag.
==28174==  The main thread stack size used in this run was 8388608.

这个线程池工作正常,我已经测试过了。

发生了一些奇怪的行为。

-如果我删除该行

  this->_writeThread = new std::thread(&Process::writeInfo, this);

和他的加入,我没有段错误。

-如果我删除 delete this->_threadPool,我仍然是 segfaut 但 valgrind 没有检测到它。

知道是什么原因造成的吗?

【问题讨论】:

  • mcve,加上所有不必要的news 和deletes,你可能违反了 0/3/5 的规则。
  • 你有没有 join-ing std::thread 两次(在 ThreadPool 的析构函数的循环中)?
  • @LogicStuff 不,这是不同的线程
  • 与您的问题无关,但当您致电 _threadList.empty() 时,您可能指的是 _threadList.clear()。以及为什么互斥锁是动态的而不是仅仅调用resize()?.
  • 好吧,如果删除 writeInfo 线程可以防止段错误,则强烈表明问题实际上可能出在函数 writeInfo() 或由它触发的某些东西中。

标签: c++ multithreading c++11 process


【解决方案1】:

我认为您的问题来自您的一个线程从未加入这一事实。验证您的整个代码并确保您加入了每个线程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多