2017-07-21 47 views
0
class MyClass{ 
    public : 
     void runBscGwThread(int); 
    private : 
     std::unique_ptr<std::thread> MyClass_; 
} 


MyClass::Myclass(int bcfId, A::AParam sctpParams): 
      bcfId_(bcfId), 
{ 
    Myclass_.reset(new std::thread([this](){ this->runBscGwThread(bcfId_);})); 
} 

偶爾會看到此問題。在析構函數中我做SIGABRT問題在valgrind中用C++中的多線程處理11

Myclass:: ~Myclass(){ 
if (Myclass_->joinable) 
    Myclass_->join(); 
} 

的Valgrind的日誌低於:

fatal_signal> 
    <tid>2</tid> 
    <signo>6</signo> 
    <signame>SIGABRT</signame> 
    <sicode>-6</sicode> 
    <stack> 
    <frame> 
     <ip>0x670DB80</ip 

/編譯/ ltesdkroot /數據/平臺/ IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/OS/SYS-根/ i686的-PC-Linux的GNU/usr/lib目錄/ libc-2.24.so

<fn>raise</fn> 
     </frame> 
    <frame> 
    <ip>0x670F046</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libc-2.24.so</obj> 
    <fn>abort</fn> 
</frame> 
<frame> 
    <ip>0x656831E</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libstdc++.so.6.0.22</obj> 
    <fn>__gnu_cxx::__verbose_terminate_handler()</fn> 
</frame> 
<frame> 
    <ip>0x6565EC3</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libstdc++.so.6.0.22</obj> 
</frame> 
<frame> 
    <ip>0x6564C28</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libstdc++.so.6.0.22</obj> 
</frame> 
<frame> 
    <ip>0x6565426</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libstdc++.so.6.0.22</obj> 
    <fn>__gxx_personality_v0</fn> 
</frame> 
<frame> 
    <ip>0x66D941E</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libgcc_s.so.1</obj> 
</frame> 
<frame> 
    <ip>0x66D974C</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libgcc_s.so.1</obj> 
    <fn>_Unwind_RaiseException</fn> 
</frame> 
<frame> 
    <ip>0x65661E3</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libstdc++.so.6.0.22</obj> 
    <fn>__cxa_throw</fn> 
</frame> 
<frame> 
    <ip>0x65913F2</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libstdc++.so.6.0.22</obj> 
    <fn>std::__throw_system_error(int)</fn> 
</frame> 
<frame> 
    <ip>0x6593917</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libstdc++.so.6.0.22</obj> 
    <fn>std::thread::join()</fn> 
</frame> 
<frame> 
    <ip>0x8083F41</ip> 
    <obj>/dev/shm/workspace/GTS+trunk.x86.VALGRIND.UT/build/x86/gts/test/ut/tests/BscGw/GTS-BscGw-UT</obj> 
    <fn>GTS::BSCGW::Myclass::stop()</fn> 
</frame> 
<frame> 
    <ip>0x8083F69</ip> 
    <obj>/dev/shm/workspace/GTS+trunk.x86.VALGRIND.UT/build/x86/gts/test/ut/tests/BscGw/GTS-BscGw-UT</obj> 
    <fn>GTS::BSCGW::Myclass::~Myclass()</fn> 
</frame> 
<frame> 
    <ip>0x807DD62</ip> 
    <obj>/dev/shm/workspace/GTS+trunk.x86.VALGRIND.UT/build/x86/gts/test/ut/tests/BscGw/GTS-BscGw-UT</obj> 
    <fn>std::__shared_count&lt;(__gnu_cxx::_Lock_policy)2&gt;::~__shared_count()</fn> 
</frame> 
<frame> 
    <ip>0x807E15E</ip> 
    <obj>/dev/shm/workspace/GTS+trunk.x86.VALGRIND.UT/build/x86/gts/test/ut/tests/BscGw/GTS-BscGw-UT</obj> 
    <fn>std::_Rb_tree&lt;int, std::pair&lt;int const, std::pair&lt;std::shared_ptr&lt;, std::shared_ptr&lt;GTS::BSCGW::Myclass&gt; &gt; &gt;, std::_Select1st&lt;std::pair&lt;int const, std::pair&lt;, std::shared_ptr&lt;GTS::BSCGW::Myclass&gt; &gt; &gt; &gt;, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;int const, std::pair&lt;std::shared_ptr&lt;, std::shared_ptr&lt;GTS::BSCGW::Myclass&gt; &gt; &gt; &gt; &gt;::_M_erase(std::_Rb_tree_node&lt;std::pair&lt;int const, std::pair&lt;std::shared_ptr&lt; std::shared_ptr&lt;GTS::BSCGW::Myclass&gt; &gt; &gt; &gt;*)</fn> 
</frame> 
<frame> 
    <ip>0x807E150</ip> 
    <obj>/dev/shm/workspace/GTS+trunk.x86.VALGRIND.UT/build/x86/gts/test/ut/tests/BscGw/GTS-BscGw-UT</obj> 
    <fn>std::_Rb_tree&lt;int, std::pair&lt;int const, std::pair&lt;std::shared_ptr&lt;, std::shared_ptr&lt;GTS::BSCGW::Myclass&gt; &gt; &gt;, std::_Select1st&lt;std::pair&lt;int const, std::pair&lt;std::shared_ptr&lt;, std::shared_ptr&lt;GTS::BSCGW::Myclass&gt; &gt; &gt; &gt;, std::less&lt;int&gt;, std::allocator&lt;std::pair&lt;int const, std::pair&lt;std::shared_ptr&lt;, std::shared_ptr&lt;GTS::BSCGW::Myclass&gt; &gt; &gt; &gt; &gt;::_M_erase(std::_Rb_tree_node&lt;std::pair&lt;int const, std::shared_ptr&lt;GTS::BSCGW::Myclass&gt; &gt; &gt; &gt;*)</fn> 
</frame> 
<frame> 
    <ip>0x8084928</ip> 
    <obj>/dev/shm/workspace/GTS+trunk.x86.VALGRIND.UT/build/x86/gts/test/ut/tests/BscGw/GTS-BscGw-UT</obj> 
    <fn>GTS::BSCGW::Myclass::readFromSocket(std::vector&lt;unsigned char, std::allocator&lt;unsigned char&gt; &gt;&amp;, int)</fn> 
</frame> 
<frame> 
    <ip>0x8085142</ip> 
    <obj>/dev/shm/workspace/GTS+trunk.x86.VALGRIND.UT/build/x86/gts/test/ut/tests/BscGw/GTS-BscGw-UT</obj> 
    <fn>GTS::BSCGW::Myclass::runBscGwThread(int)</fn> 
</frame> 
<frame> 
    <ip>0x80852A6</ip> 
    <obj>/dev/shm/workspace/GTS+trunk.x86.VALGRIND.UT/build/x86/gts/test/ut/tests/BscGw/GTS-BscGw-UT</obj> 
    <fn>std::thread::_Impl&lt;std::_Bind_simple&lt;GTS::BSCGW::Myclass::Myclass(int, GTS::AbisSctpParam)::{lambda()#1}()&gt; &gt;::_M_run()</fn> 
</frame> 
<frame> 
    <ip>0x6593A8D</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libstdc++.so.6.0.22</obj> 
</frame> 
<frame> 
    <ip>0x64DD2E9</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libpthread-2.24.so</obj> 
    <fn>start_thread</fn> 
</frame> 
<frame> 
    <ip>0x67C90ED</ip> 
    <obj>/build/ltesdkroot/data/Platforms/IB_PS_LFS_REL/IB013_PS_LFS_REL_2017_06_0017-LTE/IB013_PS_LFS_REL_2017_06_0017-LTE/os/sys-root/i686-pc-linux-gnu/usr/lib/libc-2.24.so</obj> 
    <fn>clone</fn> 
</frame> 

+0

堆棧跟蹤似乎表明您正在銷燬'MyClass :: runBscGwThread'中的'MyClass',所以這個線程試圖加入它自己。這正確地拋出'system_error' –

回答

0
Myclass:: ~Myclass(){ 
    if (Myclass_->joinable) 
     Myclass_->join(); 
    } 

由於您已將該線程保持爲可連接,因此調用線程將等待其完成。

是否需要保持線程可連接,如果沒有對資源的併發訪問,則可以允許這些線程並行運行。