?
Solved

Detect Boost recursive_mutex already acquired

Posted on 2007-10-21
2
Medium Priority
?
1,286 Views
Last Modified: 2008-01-09
I have a multi-threading problem where multiple threads access a function protected by a recursive_mutex.

When one thread has acquired the lock, I'd like other threads to simply exit and try later.  My problem is that I don't know how to determine that the mutex has already been acquired.

Here is a simple sample Windows program:

#include <windows.h>
#include <iostream>
#include <boost/thread/thread.hpp>
#include "boost/thread/recursive_mutex.hpp"

boost::recursive_mutex singleThreaded;

void ThreadProc() {
   // Want to exit if the mutex has already been acquired
   // What do I do?
   boost::recursive_mutex::scoped_lock scoped_lock(singleThreaded);
   std::cout << "Acquired the mutex" << std::endl;
   ::Sleep(500);
   std::cout << "Exit" << std::endl;
}

void main() {
   boost::thread one(&ThreadProc);
   boost::thread two(&ThreadProc);
   one.join();
   two.join();
}
0
Comment
Question by:josgood
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20118958
0
 
LVL 13

Author Comment

by:josgood
ID: 20119048
Thank you, jkr.  That works like a charm.

#include <windows.h>
#include <iostream>
#include <boost/thread/thread.hpp>
#include "boost/thread/recursive_mutex.hpp"

boost::recursive_try_mutex singleThreaded;

void ThreadProc() {
   boost::recursive_try_mutex::scoped_try_lock scoped_lock(singleThreaded);
   if (scoped_lock.locked()) {
      std::cout << "Acquired the mutex" << std::endl;
      ::Sleep(500);
   } else {
      std::cout << "Mutex has already been acquired" << std::endl;
   }
   std::cout << "Exit" << std::endl;
}

void main() {
   boost::thread one(&ThreadProc);
   boost::thread two(&ThreadProc);
   one.join();
   two.join();
}
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

862 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