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有如下代码片断:
- #if defined(HAVE_PTHREADS)
- inline Mutex::Mutex() {
- pthread_mutex_init(&mMutex, NULL);
- }
- inline Mutex::Mutex(const char* name) {
- pthread_mutex_init(&mMutex, NULL);
- }
- inline Mutex::Mutex(int type, const char* name) {
- if (type == SHARED) {
- pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
- pthread_mutex_init(&mMutex, &attr);
- pthread_mutexattr_destroy(&attr);
- } else {
- pthread_mutex_init(&mMutex, NULL);
- }
- }
- inline Mutex::~Mutex() {
- pthread_mutex_destroy(&mMutex);
- }
- inline status_t Mutex::lock() {
- return -pthread_mutex_lock(&mMutex);
- }
- inline void Mutex::unlock() {
- pthread_mutex_unlock(&mMutex);
- }
- inline status_t Mutex::tryLock() {
- return -pthread_mutex_trylock(&mMutex);
- }
- #endif // HAVE_PTHREADS
