【发布时间】:2014-11-25 01:42:03
【问题描述】:
此段错误:
std::vector<std::thread> _pool;
State & _state;
...
for(uint32_t n = 0; n < nThreads; ++n)
_pool[n] = std::thread(_thFunction, std::ref(_state));
这不是:
std::vector<std::thread> _pool;
State & _state;
...
for(uint32_t n = 0; n < nThreads; ++n)
_pool.push_back( std::thread(_thFunction, std::ref(_state)) );
对向量使用 push_back 而不是对向量中的特定条目进行赋值的区别。
_thFunction 是一个 std::function。
当我将 pool.reserve(10) 应用到第一个代码块时,我仍然在第一个分配上得到一个段错误。
我怀疑这与移动语义有关,但我不确定。这是怎么回事?
来自 gdb 的堆栈跟踪似乎表明 this 指针为空:
Program received signal SIGSEGV, Segmentation fault.
0x00000000004092e5 in std::thread::joinable (this=0x0) at /usr/include/c++/4.8.3/thread:162
162 { return !(_M_id == id()); }
(gdb) backtrace
#0 0x00000000004092e5 in std::thread::joinable (this=0x0) at /usr/include/c++/4.8.3/thread:162
#1 0x000000000040927a in std::thread::operator=(std::thread&&) (this=0x0,
__t=<unknown type in /home/stackuser/src/dsl/build/debug/server/dsl, CU 0x0, DIE 0x37b88>)
at /usr/include/c++/4.8.3/thread:150
#2 0x000000000041350d in ThreadPool<std::function<void (State&)> >::ThreadPool(State&, unsigned int, std::function<void (State&)>) (this=0x69aed0, state=..., nThreads=2, thFunction=...)
at src/server/ThreadPool.h:38
#3 0x0000000000408405 in Application::Application (this=0x6946e0) at src/server/Application.cpp:69
#4 0x0000000000455594 in Singleton<Application>::CreateInstance () at src/server/Singleton.h:12
#5 0x0000000000454997 in main (argc=1, argv=0x7fffffffdc98) at src/server/main.cpp:85
【问题讨论】:
-
保留不会为您提供可供使用的空间!!
标签: c++ multithreading c++11 segmentation-fault