在boost.thread的启动功能,源代码是类似的东西:为什么“boost.thread”手动调用“intrusive_ptr_add_ref”?
bool thread::start_thread_noexcept()
{
uintptr_t const new_thread = _beginthreadex(
0,
0,
&thread_start_function,
thread_info.get(),
CREATE_SUSPENDED,
&thread_info->id);
if (!new_thread)
{
return false;
}
// why call this line?
intrusive_ptr_add_ref(thread_info.get());
thread_info->thread_handle = (detail::win32::handle)(new_thread);
ResumeThread(thread_info->thread_handle);
return true;
}
的thread_info是一个侵入智能指针,它指向线程信息数据,调用intrusive_ptr_add_ref之前,计数已经是1,我不知道为什么在这里手动调用intrusive_ptr_add_ref。我认为Intrusive智能指针的工作应该是自动调用intrusive_ptr_add_ref和intrusive_ptr_release。
我试过一步一步通过源代码,但没有找到任何线索。
谁能告诉我 1.为什么要在这里手动调用intrusive_ptr_add_ref? 2.在使用intrusive_ptr的情况下,我应该手动调用intrusive_ptr_add_ref?
谢谢,真诚。
非常感谢!你的回答非常有价值,并且揭示了一切! – zach