user1479830
user1479830

Reputation: 1

POSIX SHARED - RECURSIVE Mutex

I'm working on linux. In my code I'm trying to run a few threads ( lets say 2 for example) that are trying to lock a RECURSIVE mutex, but only one thread can access and lock the mutex while the second one gets an EBUSY error even after the first thread has unlocked it. I think it's because the mutex is PRIVET and not SHARED.

I'm trying to set the mutex to be both RECRUSIVE and SHARED like this:

    void
MutexCreate(pthread_mutex_t* _m)
{
    pthread_mutexattr_t attr; 

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(_m, &attr); 
}

(I do check for function errors - and all of them return 0)

Even if I try to make it a DEFAULT SHARED mutex by :

    void
MutexCreate(pthread_mutex_t* _m)
{
    pthread_mutexattr_t attr; 

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
    pthread_mutex_init(_m, &attr); 
}

it still remains PRIVET.

Any ideas?

Upvotes: 0

Views: 887

Answers (1)

Jens Gustedt
Jens Gustedt

Reputation: 78903

If you really have two threads inside the same process the pshared attribute shouldn't have an influence.

EBUSY is only permitted as a return for pthread_mutex_trylock, so I suppose you used this.

The only explanation that I have is that you maybe locked your mutex several times with the first thread. lock and unlock always come in pairs, make sure that you have as many unlocks as you have locks.

Upvotes: 1

Related Questions