Solved

QT Recursive Mutex

Posted on 2004-03-30
8
1,083 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to import linux vhd to sccvmm 1 150
Bash Script to find & Kill Multiple Process 3 94
What is the timeframe of this cron job? 2 131
what is this linux system is? 16 117
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now