Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当'this'变成null ptr时,如何处理类成员线程中的快速退出?_C++_Thread Safety_Terminate - Fatal编程技术网

C++ 当'this'变成null ptr时,如何处理类成员线程中的快速退出?

C++ 当'this'变成null ptr时,如何处理类成员线程中的快速退出?,c++,thread-safety,terminate,C++,Thread Safety,Terminate,我有一小段代码: // (...) class Time { std::atomic<bool> m_running; std::thread m_worker; // ... }; Time::Time() { // ... m_running = true; m_worker = std::move(std::thread(std::bind(&Time::Worker, this))); } bool Time::HasTimedOu

我有一小段代码:

// (...)
class Time 
{
  std::atomic<bool> m_running;
  std::thread m_worker;
  // ...
};


Time::Time()
{
  // ...
  m_running = true;
  m_worker  = std::move(std::thread(std::bind(&Time::Worker, this)));
}


bool Time::HasTimedOut() const
{
  return (!m_disabled) &&
         (IsPending() && (GetRunTime() >= m_maximum_timeout) && (CloseHandlesDiff() >= m_minimum_close_time));
}

Time::~Time()
{
  if (m_running)
  {
    m_running = false;
    m_worker.join();
  }
}

void Time::Worker()
{
  while (m_running)
  {
    if (time_data->HasTimedOut())
    {
      time_data->RunTimedOutCallback();
    }

    if (m_count < 0)
    {
      m_count = 0;
    }

    if (m_running)
    {
      std::this_thread::sleep_for(std::chrono::milliseconds(20));
    }
  }
}

std::shared_ptr<Time> time_data(std::make_shared<Time>());

您的代码具有竞争条件

时间 { // ... m_running=true; m_worker=std::movestd::threadstd::bind&Time::worker,这个; } 这里的踏板开始之前

std::shared_ptr time_datastd::make_shared; 完成了

只需线程在std::shared\u ptr Time\u datastd::make\u shared之前到达monitor::Time::hastinmedout;完成了

产卵线程不是在构造函数中,而是在分配时间_数据后将调用的单独方法中


无论如何,如果您的计时器根本不使用time\U data全局变量,那就更好了。

您的代码有竞争条件

时间 { // ... m_running=true; m_worker=std::movestd::threadstd::bind&Time::worker,这个; } 这里的踏板开始之前

std::shared_ptr time_datastd::make_shared; 完成了

只需线程在std::shared\u ptr Time\u datastd::make\u shared之前到达monitor::Time::hastinmedout;完成了

产卵线程不是在构造函数中,而是在分配时间_数据后将调用的单独方法中


无论如何,如果您的计时器根本不使用time\U data全局变量,那就更好了。

这意味着指针被分配了nullptr,然后对该指针调用了方法。请提供更多代码和更多回调堆栈跟踪项,因为错误隐藏得更深。更多代码只是std::bind从std::thread调用工作线程。更多的代码可能会污染这个问题。也许我也会添加构造函数。是的,这一个-显示它!这个问题很可能和指针的生命周期有关。你们在哪里设置时间数据的值?时间数据是一个全局变量吗?也许您应该添加一个deleter函数,该函数在删除线程时终止该线程?这意味着指针被分配了nullptr,然后对该指针调用了方法。请提供更多代码和更多回调堆栈跟踪项,因为错误隐藏得更深。更多代码只是std::bind从std::thread调用工作线程。更多的代码可能会污染这个问题。也许我也会添加构造函数。是的,这一个-显示它!这个问题很可能和指针的生命周期有关。你们在哪里设置时间数据的值?时间数据是一个全局变量吗?也许你应该添加一个删除函数,当线程被删除时,它会终止线程?哦,如果是这样的话,那么我犯了一个非常愚蠢的错误。我会仔细检查解决方案。我意识到它不需要使用复制粘贴错误。我已经确认这是可行的,但我仍然想知道,当析构函数没有被调用时,应用程序是否可以保护自己免受SEGFAULT的影响。也许我会再问一次,什么时候可以用std::quick_exit展示一个工作示例。正在努力。哦,如果是这样的话,那么我犯了一个非常愚蠢的错误。我会仔细检查解决方案。我意识到它不需要使用复制粘贴错误。我已经确认这是可行的,但我仍然想知道,当析构函数没有被调用时,应用程序是否可以保护自己免受SEGFAULT的影响。也许我会再问一次,什么时候可以用std::quick_exit展示一个工作示例。正在努力。
(gdb) bt
#0  0x09438408 in monitor::Time::HasTimedOut (this=0x0)
    at monitor.cxx: // return (!m_disabled) &&
#1  0x09438a84 in monitor::Time::Worker (this=0xbd96dd8)
    monitor.cxx: // if(time_data->HasTimedOut()
#2  0x0943cf81 in std::__invoke_impl<void, void (monitor::Time::*&)(), monitor::Time*&> (
    __f=@0xbd96e54: (void (monitor::Time::*)(monitor::Time * const)) 0x94389f0 <monitor::Time::Worker()>, __t=@0xbd96e5c: 0xbd96dd8)
(gdb) bt
(gdb) bt
#0  0x09438408 in monitor::Time::HasTimedOut (this=0x0)
    at /opt/procesleiding/vptlib/lib/oracle_monitor.cxx:111
#1  0x09438a84 in monitor::Time::Worker (this=0xbd96dd8)
    at /opt/procesleiding/vptlib/lib/oracle_monitor.cxx:142
#2  0x0943cf81 in std::__invoke_impl<void, void (monitor::Time::*&)(), monitor::Time*&> (
    __f=@0xbd96e54: (void (monitor::Time::*)(monitor::Time * const)) 0x94389f0 <monitor::Time::Worker()>, __t=@0xbd96e5c: 0xbd96dd8)
    at /usr/include/c++/7/bits/invoke.h:73
#3  0x0943c99f in std::__invoke<void (monitor::Time::*&)(), monitor::Time*&> (
    __fn=@0xbd96e54: (void (monitor::Time::*)(monitor::Time * const)) 0x94389f0 <monitor::Time::Worker()>, __args#0=@0xbd96e5c: 0xbd96dd8)
    at /usr/include/c++/7/bits/invoke.h:95
#4  0x0943c70c in std::_Bind<void (monitor::Time::*(monitor::Time*))()>::__call<void, , 0u>(std::tuple<>&&, std::_Index_tuple<0u>) (
    this=0xbd96e54, __args=...)
    at /usr/include/c++/7/functional:467
#5  0x0943c28c in std::_Bind<void (monitor::Time::*(monitor::Time*))()>::operator()<, void>() (this=0xbd96e54)
    at /usr/include/c++/7/functional:551
#6  0x0943bcaf in std::__invoke_impl<void, std::_Bind<void (monitor::Time::*(monitor::Time*))()>>(std::__invoke_other, std::_Bind<void (monitor::Time::*(monitor::Time*))()>&&) (__f=...) at /usr/include/c++/7/bits/invoke.h:60
#7  0x0943b022 in std::__invoke<std::_Bind<void (monitor::Time::*(monitor::Time*))()>>(std::_Bind<void (monitor::Time::*(monitor::Time*))()>&&) (__fn=...) at /usr/include/c++/7/bits/invoke.h:95
#8  0x0943e2a6 in std::thread::_Invoker<std::tuple<std::_Bind<void (monitor::Time::*(monitor::Time*))()> > >::_M_invoke<0u>(std::_Index_tuple<0u>) (this=0xbd96e54) at /usr/include/c++/7/thread:234
#9  0x0943e15c in std::thread::_Invoker<std::tuple<std::_Bind<void (monitor::Time::*(monitor::Time*))()> > >::operator()() (this=0xbd96e54) at /usr/include/c++/7/thread:243
#10 0x0943e067 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<std::_Bind<void (monitor::Time::*(monitor::Time*))()> > > >::_M_run() (this=0xbd96e50) at /usr/include/c++/7/thread:186