Solved

QT Recursive Mutex

Posted on 2004-03-30
8
1,120 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.
0
Comment
Question by:jeffreyg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
8 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10712893
Hi jeffreyg,

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

Cheers,
Stefan
0
 

Author Comment

by:jeffreyg
ID: 10713632
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.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10714574
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:stefan73
ID: 10714673
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?
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10714704
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).
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 125 total points
ID: 10714764
This works fine in cygwin:

#include <pthread.h>
#include <errno.h>
#include <assert.h>

pthread_mutex_t criticalMutex;

int createMyMutex(pthread_mutex_t *cs);

void recurse(int level){
      printf("Level %d - locking mutex\n",level);
      assert(pthread_mutex_lock(&criticalMutex)==0);
      if(level>0)
            recurse(level-1);
      printf("Level %d - unlocking mutex\n",level);
      assert(pthread_mutex_unlock(&criticalMutex)==0);
}

int main(){
      int res = createMyMutex(&criticalMutex);
      recurse(5);
      pthread_mutex_destroy(&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");
  else if (didItSet == EFAULT)
    printf("Invalid attribute pointer\n");

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


Output is:
$ ./mutextest
Managed to set the attributes
Level 5 - locking mutex
Level 4 - locking mutex
Level 3 - locking mutex
Level 2 - locking mutex
Level 1 - locking mutex
Level 0 - locking mutex
Level 0 - unlocking mutex
Level 1 - unlocking mutex
Level 2 - unlocking mutex
Level 3 - unlocking mutex
Level 4 - unlocking mutex
Level 5 - unlocking mutex

Check if it works in Linux, too.
0
 

Author Comment

by:jeffreyg
ID: 10723731
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.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10724076
Welcome :-)

Check if the above code also works with pthread_mutex_trylock().
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
understanding an rpm spec file 5 68
Replication of Nagios surveilance data 2 103
Embeded Linux on Router 9 112
Why  my code (program) build with old compiler? 11 149
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question