Use c++11 std::thread instead of pthread.

This commit is contained in:
Matthieu Gautier 2021-01-20 16:41:22 +01:00
parent 50314fdae8
commit da247042c0
2 changed files with 5 additions and 21 deletions

View File

@ -28,11 +28,7 @@
#include "utils.h" #include "utils.h"
#if __ANDROID__ std::mutex globalLock;
pthread_mutex_t globalLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
#else
pthread_mutex_t globalLock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
#endif
JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory( JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory(
JNIEnv* env, jclass kclass, jstring dirStr) JNIEnv* env, jclass kclass, jstring dirStr)

View File

@ -24,7 +24,7 @@
#include <jni.h> #include <jni.h>
#include <pthread.h> #include <mutex>
#include <string> #include <string>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
@ -36,7 +36,7 @@
#define LOG(...) #define LOG(...)
#endif #endif
extern pthread_mutex_t globalLock; extern std::mutex globalLock;
template<typename T> template<typename T>
void setPtr(JNIEnv* env, jobject thisObj, T* ptr) void setPtr(JNIEnv* env, jobject thisObj, T* ptr)
@ -88,22 +88,10 @@ inline jobjectArray createArray(JNIEnv* env, size_t length, const std::string& t
return env->NewObjectArray(length, c, NULL); return env->NewObjectArray(length, c, NULL);
} }
class Lock class Lock : public std::unique_lock<std::mutex>
{ {
protected:
pthread_mutex_t* lock;
public: public:
Lock() : lock(&globalLock) { pthread_mutex_lock(lock); } Lock() : std::unique_lock<std::mutex>(globalLock) { }
Lock(const Lock&) = delete;
Lock& operator=(const Lock&) = delete;
Lock(Lock&& other) : lock(&globalLock) { other.lock = nullptr; }
virtual ~Lock()
{
if (lock) {
pthread_mutex_unlock(lock);
}
}
}; };
template <class T> template <class T>