手机版
你好,游客 登录 注册
背景:
阅读新闻

使用C++实现JNI接口需要注意的事项

[日期:2011-05-31] 来源:Linux社区  作者:feifei454498130 [字体:大 中 小]

3. 因业务需求,在JNI C++代码中需要用到多线程,同时加入了互斥锁等机制,编译时出现如下问题:

/home/chenzhengyong/Android/source/eclair/frameworks/base/include/utils/threads.h:214: undefined reference to `android::Mutex::lock()'

察看了一下ndk中的STABLE-APIS.TXT文档,上面有这样一句:

Note that the Android C library includes support for pthread (<pthread.h>),

so "LOCAL_LIBS := -lpthread" is not needed. The same is true for real-time

extensions (-lrt on typical Linux distributions).

也就是说使用多线程,并不需要增加-lpthread编译选项,看来解决问题还需要从源代码入手。察看frameworks/base/include/utils/threads.h文件,关于Mutex有如下代码片断:

  1. #if defined(HAVE_PTHREADS)   
  2. inline Mutex::Mutex() {   
  3.     pthread_mutex_init(&mMutex, NULL);   
  4. }   
  5. inline Mutex::Mutex(const char* name) {   
  6.     pthread_mutex_init(&mMutex, NULL);   
  7. }   
  8. inline Mutex::Mutex(int type, const char* name) {   
  9.     if (type == SHARED) {   
  10.         pthread_mutexattr_t attr;   
  11.         pthread_mutexattr_init(&attr);   
  12.         pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);   
  13.         pthread_mutex_init(&mMutex, &attr);   
  14.         pthread_mutexattr_destroy(&attr);   
  15.     } else {   
  16.         pthread_mutex_init(&mMutex, NULL);   
  17.     }   
  18. }   
  19. inline Mutex::~Mutex() {   
  20.     pthread_mutex_destroy(&mMutex);   
  21. }   
  22. inline status_t Mutex::lock() {   
  23.     return -pthread_mutex_lock(&mMutex);   
  24. }   
  25. inline void Mutex::unlock() {   
  26.     pthread_mutex_unlock(&mMutex);   
  27. }   
  28. inline status_t Mutex::tryLock() {   
  29.     return -pthread_mutex_trylock(&mMutex);   
  30. }   
  31. #endif // HAVE_PTHREADS  
linux
相关资讯       C++ 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款