Android NDK Mutex

I am trying to do some multithreading using the Android Native Development Kit, so I need a mutex on the c++ side.

What’s the proper way to create and use a mutex with Android NDK?

  • Setting Request Priority Volley
  • YUV to RGB conversion by fragment shader
  • How to convert .jar or .class to .dex file?
  • How to: Define theme (style) item for custom widget
  • Is there a Dropped Call Listener
  • Load Android image in React Native
  • Thanks

    Related posts:

    Android Studio installation on Windows 7 fails, no JDK found
    Android SharedPreference security
    Strange NoClassDefFoundError error when launching a signed release APK with proguard enabled
    Disable Button click sound in android
    Android PackageStats gives negative Values
    Autoplay youtube videos in android
  • How to programmatically set the width of the LinearLayout?
  • Android Toolbar moves up when keyboard appears
  • Android - how to set background color of all screens?
  • What would happen if Android app is released with debuggable on?
  • What in TtsService could explain the lack of onUtteranceCompleted() for playEarcon()?
  • Android Content Provider and Many-to-Many DB relationship
  • 3 Solutions collect form web for “Android NDK Mutex”

    The NDK appears to have support for pthread mutexes. I’ve not made use of them myself.

    Here is how we go on Windows and Android (OS_LINUX define is for Android):

    class clMutex
    {
    public:
        clMutex()
        {
    #ifdef OS_LINUX
            pthread_mutex_init( &TheMutex, NULL );
    #endif
    #ifdef OS_WINDOWS
            InitializeCriticalSection( &TheCS );
    #endif
        }
    
        /// Enter the critical section -- other threads are locked out
        void Lock() const
        {
    #ifdef OS_LINUX
            pthread_mutex_lock( &TheMutex );
    #endif
    #ifdef OS_WINDOWS
    
            if ( !TryEnterCriticalSection( &TheCS ) ) EnterCriticalSection( &TheCS );
    #endif
        }
    
        /// Leave the critical section
        void Unlock() const
        {
    #ifdef OS_LINUX
            pthread_mutex_unlock( &TheMutex );
    #endif
    #ifdef OS_WINDOWS
            LeaveCriticalSection( &TheCS );
    #endif
        }
    
        ~clMutex()
        {
    #ifdef OS_WINDOWS
            DeleteCriticalSection( &TheCS );
    #endif
    #ifdef OS_LINUX
            pthread_mutex_destroy( &TheMutex );
    #endif
        }
    
    #ifdef OS_LINUX
        // POSIX threads
        mutable pthread_mutex_t TheMutex;
    #endif
    #ifdef OS_WINDOWS
        mutable CRITICAL_SECTION TheCS;
    #endif
    };
    

    As one of the developers of Linderdaum Engine i recommend checking out for Mutex.h in our SDK.

    #include <pthread.h>
    
    class CThreadLock  
    {
    public:
        CThreadLock();
        virtual ~CThreadLock();
    
        void Lock();
        void Unlock();
    private:
        pthread_mutex_t mutexlock;
    };
    
    CThreadLock::CThreadLock()
    {
        // init lock here
        pthread_mutex_init(&mutexlock, 0);
    }
    
    CThreadLock::~CThreadLock()
    {
        // deinit lock here
        pthread_mutex_destroy(&mutexlock);
    }
    void CThreadLock::Lock()
    {
        // lock
        pthread_mutex_lock(&mutexlock);
    }
    void CThreadLock::Unlock()
    {
        // unlock
        pthread_mutex_unlock(&mutexlock);
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.