std :: thread作为执行随机线程的类的成员,导致在asignate线程

时间:2015-09-30 05:06:58

标签: android c++ multithreading c++11 android-ndk

在我的班级中,我将线程对象作为公共元素:

class AppManager{
   ...
    public:
    std::thread m_thread;
};

然后当我初始化线程时:

void* BridgeFunction(void *pctx) {
    ((AppManager*)pctx)->MainAppThread();
    return 0;
}

void AppManager::CreateAppThread(){
   m_thread = std::thread(&BridgeFunction, this);
   m_thread.detach();
}

我在std :: terminate上有一个Abort:

 thread& operator=(thread&& __t) noexcept
    {
      if (joinable())
      std::terminate();
      swap(__t);
      return *this;
    }

为什么呢?我打电话给分离,我尝试改变:

m_thread = std::thread(&BridgeFunction, this);

要:

m_thread = std::thread(&AppManager::MainAppThread, this);

同样,它只有在我将m_thread声明为全局并且我调试它为什么工作时才有效,事实证明声明为成员的线程执行随机线程之前我分配线程,I知道这个因为我打印了id以查看它是否可以连接:

    auto myid = m_thread.get_id();
    std::stringstream ss;
    ss << myid;
    std::string thread_id = ss.str();

    LogPrintDebug("[Thread Activity] -  Id of thread not created yet: %s", thread_id.c_str());

    m_thread = std::thread(&BridgeFunction, this);
    m_thread.detach();

打印:

[Thread Activity] - Id of thread not created yet: 1074643408

当我声明全局它打印的线程时:

[Thread Activity] - Id of thread not created yet::id of a non-executing thread

我还打印了我的实际线程的ID,它与随机线程的Id不同,因此尝试终止它由于该部分:

if (joinable())
 std::terminate();

我使用Visual Studio 2015使用Clang 3.6和GNU STL库运行Android Native Activity,ndk r10e。

我在Windows上测试了相同的代码,并且线程的ID为0,没有任何中止。

由于

0 个答案:

没有答案
相关问题