We help IT Professionals succeed at work.

QT Recursive Mutex

jeffreyg
jeffreyg asked
on
Medium Priority
1,337 Views
Last Modified: 2008-02-01
Guys,

Using QT 3.1.1.

I am trying to create a mutex in linux that mimics the behaviour of Windows i.e. if we are in a mutex lock and we try and lock the mutex again from inside the thread this is allowed. I think this is called recursive mutexing and this stops dead-locking the application.

I have tried the following when creating my mutex to make it mimic the Windows mutex:

pthread_mutex_t *criticalSection = cs;

pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);

if (pthread_mutex_init(criticalSection, &attr) == 0)
  // success

and when trying to lock the mutex:

if (pthread_mutex_trylock(criticalSection) == 0)
  // we have managed to lock the mutex

But to no avail. It still comes back saying the mutex is busy when I try and lock it a second time. The recursive setup must not be working.

Any ideas what I'm doing wrong?

regards,

Gordon.
Comment
Watch Question

Top Expert 2004

Commented:
Hi jeffreyg,

Are you sure cs points to a valid, initialized mutex?

Cheers,
Stefan

Author

Commented:
Yeah,

In my main code I have

pthread_mutex_t criticalMutex;
int res = createMyMutex(&criticalMutex);

....

int createMyMutex(pthread_mutex_t *cs)
{
  pthread_mutex_t *criticalSection = cs;

  pthread_mutexattr_t attr;
  pthread_mutexattr_init(&attr);

  int didItSet = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  if (didItSet == 0)
    printf("Managed to set the attributes\n");
  else if (didItSet == EINVAL)
    printf("Not initialised mutex attribute or not valid mutex type setting\n");
  els if (didItSet == EFAULT)
    printf("Invalid attribute pointer\n")

  if (pthread_mutex_init(criticalSection, &attr) == 0)
    return 0;    // Success
  else
    return -1;  // Failure
}

It says that it managed to set the attributes and then when I init the mutex with thes attributes the result "res" is 0. Successful return value is 0 isn't it?

The thread definately works as I lock it and try and lock it in other parts of my code and it works as it should, reporting busy until it can grab the mutex and continue on. The problem only occurs with the recursive calls. I have read somewhere that Linux does not default to recursive mutexing unlike Windows.

regards,
G.
Top Expert 2004

Commented:
jeffreyg,
> Linux does not default to recursive mutexing unlike Windows

For portable programs never, ever rely on the PTHREAD_MUTEX_DEFAULT type!

But it's OK to set the mutex type. However, you've only created a pthread_mutexattr_t, and not modified the mutex itself. You should also call

pthread_mutex_init(criticalSection, &attr);

...otherwise your nice mutex attribute is orphaned.



Stefan
Top Expert 2004

Commented:
That's weird. A recursive mutex should be non-blocking when the same thread calls it repeatedly. But you said it does? Did you check if it behaves correctly in recursion when you do lock instead of trylock?
Top Expert 2004

Commented:
To be more precise: It may be that trylock returns EBUSY, because there is already a lock on the mutex, even if it is the same thread which holds the lock (and the mutex is recursive).
Top Expert 2004
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Well,

It doesn't quite work in my application, but I tested out your app in linux and it works perfectly so I must be doing something else stupid. So thanks very much for the help and the points are yours.

Will keep plodding on!

Cheers mate!!!!!

G.
Top Expert 2004

Commented:
Welcome :-)

Check if the above code also works with pthread_mutex_trylock().
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.