vendredi 1 mars 2019

android ndk c++ 11 locking std::mutex

I'm trying to lock a mutex on a thread, the problem is that another thread which also uses the same mutex is locked waiting for a semaphore to be signaled. As a example:

//start thread 1 on a loop and reamains waiting for a semaphre to be signaled.

void start_thread_1() {

this->mThread = std::thread([&] {
    while (true)
    {

        // this blocks the thread until semaphore is signaled
        semaphore.wait();

        // adquire myMutex
        std::lock_guard<std::mutex> lock(myMutex);

        // some task
        some_processing();

    }
});

}

thread 2 at some point after an user action, tries to acquire myMutex but the routine gets stuck, also if I use try_lock it always returns false.

void acquire_mutex_on_thread_2 () {

// code stucks here
std::lock_guard<std::mutex> lock(myMutex);

// code never reach this
some_processing();

}

If I continously signal semaphore, lets say as example, every 100 ms, eventually the mutex is acquired on thread 2

Semaphore implementation is quite standard, uses a mutex, a condition adn a counter.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire