Solved

QT Recursive Mutex

Posted on 2004-03-30
8
1,067 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

12 Experts available now in Live!

Get 1:1 Help Now