2016-04-23 31 views
1

我有這樣的方法:線程加入導致段故障時,另一個線程運行

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->_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()處發生segfault。

我宣佈我的線程池是這樣的:

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); 

和他的加盟,我不段錯誤。

- 如果我刪除刪除this - > _ threadPool,我仍然segfaut但valgrind不檢測它。

有什麼想法可以造成這種情況?

+0

[mcve](http://stackoverflow.com/help/mcve),所有那些不必要的'new's和'delete's你可能違反了0/3/5的規則。 – user657267

+0

你是否'連接''std :: thread'兩次(在ThreadPool的析構函數的循環中)? – LogicStuff

+0

@LogicStuff不,它是不同的線程 –

回答

1

我認爲你的問題來自於你的一個線程永遠不會加入的事實。驗證你的整個代碼,並確保你加入每一個線程。

相關問題