Android NDK 互斥体

发布于 2024-11-10 03:10:02 字数 113 浏览 3 评论 0原文

我正在尝试使用 Android Native Development Kit 进行一些多线程处理,因此我需要 C++ 端的互斥锁。

通过 Android NDK 创建和使用互斥锁的正确方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑疤怪 2024-11-17 03:10:02

NDK 似乎支持 pthread 互斥体。我自己没有使用过它们。

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

云醉月微眠 2024-11-17 03:10:02

以下是我们在 Windows 和 Android 上的操作方式(OS_LINUX 定义适用于 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
};

作为 Linderdaum Engine 的开发人员之一我建议查看我们的 SDK 中的 Mutex.h。

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.

注定孤独终老 2024-11-17 03:10:02

这个问题得到解答已经有一段时间了,但我想指出,Android NDK 现在支持 C++11 及更高版本,因此现在可以使用 std:: threadstd::mutex 而不是 pthreads,下面是一个示例:

#include <thread>
#include <mutex>

int count = 0;
std::mutex myMutex;

void increment_count() {
    std::lock_guard<std::mutex> lock(myMutex);

    // Safely increment count
    count++

    // std::mutex gets unlocked when it goes out of scope
}

void JNICALL package_name_class_runMutexExample() {
    // Start 2 threads
    std::thread myThread1(increment_count);
    std::thread myThread2(increment_count);

    // Join your threads
    myThread1.join();
    myThread2.join();
}

It's been a while since this questions was answered, but I would like to point out that the Android NDK now supports C++11 and beyond, so it is now possible to use std::thread and std::mutex instead of pthreads, here's an example:

#include <thread>
#include <mutex>

int count = 0;
std::mutex myMutex;

void increment_count() {
    std::lock_guard<std::mutex> lock(myMutex);

    // Safely increment count
    count++

    // std::mutex gets unlocked when it goes out of scope
}

void JNICALL package_name_class_runMutexExample() {
    // Start 2 threads
    std::thread myThread1(increment_count);
    std::thread myThread2(increment_count);

    // Join your threads
    myThread1.join();
    myThread2.join();
}
两仪 2024-11-17 03:10:02
#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);
}
#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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文