2013-07-30 74 views
1

我有一个应用程序使用NDK来与Java应用程序一起使用共享库。共享库使用下面的线程,并且管理库中的AttachCurrentThread/DetachCurrentThread非常尴尬,所以我试图使用boost :: thread_specific_ptr创建一个管理JNIEnv *的类。问题是如果我使用boost :: thread_specific_ptr在所有应用程序几秒钟后挂起。有什么建议么?!?!为什么boost :: thread_specific_ptr在几秒钟后冻结了android ndk

更新:我添加了第二种方法,我相信这是非常相似的引擎盖下,但也有同样的问题。也有gdb的堆栈跟踪:

boost::detail::thread_data_base::~thread_data_base() at thread.cpp:42 0x72e91b74  
boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, XXX>, boost::_bi::list1<boost::_bi::value<XXX*> > > >::~thread_data() at thread.hpp:91 0x72d69198  
boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, XXX>, boost::_bi::list1<boost::_bi::value<XXX*> > > >::~thread_data() at thread.hpp:91 0x72d691e4  
boost::checked_delete<boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, XXX>, boost::_bi::list1<boost::_bi::value<XXX*> > > > >() at checked_delete.hpp:34 0x72d69230 
boost::detail::sp_counted_impl_p<boost::detail::thread_data<boost::_bi::bind_t<void, boost::_mfi::mf0<void, XXX>, boost::_bi::list1<boost::_bi::value<XXX*> > > > >::dispose() at sp_counted_impl.hpp:78 0x72d72c54 
boost::detail::sp_counted_base::release() at sp_counted_base_spin.hpp:103 0x72c67480  
boost::detail::shared_count::~shared_count() at shared_count.hpp:371 0x72c67550 
~shared_ptr() at shared_ptr.hpp:328 0x72e91a3c 
boost::thread::join_noexcept() at thread.cpp:340 0x72e91a3c 
boost::thread::join() at thread.hpp:751 0x72cda274 

的问题似乎在于在析构函数,其中i - > _ M_current为空,仍通过提升代码的工作我的方式,看看有什么可能是真正的问题,但它是缓慢的运动。

thread_data_base::~thread_data_base() 
    { 
     for (notify_list_t::iterator i = notify.begin(), e = notify.end(); 
       i != e; ++i) 
     { 
      i->second->unlock(); 
      i->first->notify_all(); 
     } 
     for (async_states_t::iterator i = async_states_.begin(), e = async_states_.end(); 
       i != e; ++i) 
     { 
      (*i)->make_ready(); 
     } 
    } 

首先尝试

class ThreadJNIEnv 
{ 

private: 

    bool  m_bDetach; 

    JNIEnv*  m_pJavaEnv; 

public: 

    ThreadJNIEnv() : 
     m_pJavaEnv(NULL) 
    { 
     LOGI("Attaching Thread"); 

     g_pJavaVM->AttachCurrentThread(&m_pJavaEnv, NULL); 

     m_bDetach = true; 
    } 

    ThreadJNIEnv(JNIEnv* pJavaEnv) : 
     m_pJavaEnv(pJavaEnv) 
    { 
     LOGI("Attach (main thread): %p ", m_pJavaEnv); 

     m_bDetach = false; 
    } 

    ~ThreadJNIEnv() 
    { 
     if(m_bDetach) 
     { 
      LOGI("Detaching Thread"); 

      g_pJavaVM->DetachCurrentThread(); 
     } 
    } 

    JNIEnv* GetEnv() { return m_pJavaEnv; }; 
}; 


// Class which manages JNIEnv per thread 
boost::thread_specific_ptr<ThreadJNIEnv> g_oJNIEnv; 

// Sample thread local data which also hangs the app 
boost::thread_specific_ptr<int> g_oValue; 

JNIEnv* GetJNIEnv() 
{ 
    // Do we already have a JNIEnv? 
    ThreadJNIEnv* pJNIEnv = g_oJNIEnv.get(); 

    if(pJNIEnv == NULL) 
    { 
     // Create a new JNIEnv (attach the thread) 
     g_oJNIEnv.reset(new ThreadJNIEnv()); 
    } 

    return g_oJNIEnv->GetEnv(); 
} 

jint JNI_OnLoad(JavaVM* vm, void* reserved) 
{ 
    g_pJavaVM = vm; 

    g_pJavaVM->GetEnv((void **)&g_pJniEnv, JNI_VERSION_1_6); 

    // This is a simple one that also exhibits the problem 
    g_oValue.reset(new int[10]); 

    // Add the main thread environment 
    //g_oJNIEnv.reset(new ThreadJNIEnv(pJavaEnv)); 

    LOGI("JNI_OnLoad called: vm=%p, env=%p", g_pJavaVM, g_pJniEnv); 

    return JNI_VERSION_1_6; 
} 

第二次尝试

void DetachThread() 
{ 
    LOGI("Detaching Thread"); 

    g_pJavaVM->DetachCurrentThread(); 
} 

JNIEnv* GetJNIEnv() 
{ 
    JNIEnv* pJNIEnv = NULL; 
    int status = g_pJavaVM->GetEnv((void **)&pJNIEnv, JNI_VERSION_1_6); 

    switch(status) 
    { 
     case JNI_EDETACHED: 
     { 
      LOGI("Attaching Thread"); 

      g_pJavaVM->AttachCurrentThread(&pJNIEnv, NULL); 

      boost::this_thread::at_thread_exit(DetachThread); 
     } 
     break; 

     case JNI_OK: 
     { 
      // Everything is ok 
     } 
     break; 

     case JNI_EVERSION: 
     { 
      LOGE("GetEnv: version not supported"); 
     } 
     break; 
    } 

    LOGI("GetJNIEnv: %p", pJNIEnv); 

    return pJNIEnv; 
} 
+0

添加了可以清楚显示问题的Android项目:https://github.com/dantwinkler/android-threading-jni。 – danw

+0

将它缩小了一点,只是提升线程加入。工作示例位于:https://github.com/dantwinkler/android-threading-jni/tree/boost-thread – danw

+0

您是否尝试启用完整的CheckJNI? https://developer.android.com/training/articles/perf-jni.html#extended_checking – Delyan

回答

0

根据堆栈跟踪,如果我正确地理解的,共享指针的破坏,包含螺纹,从开始join,这似乎很奇怪。你的github代码对我来说确实很好,但如果问题出现在boost版本上,请尽量避免使用共享指针,并在其中使用scoped_thread或移动构造。