10

For my cross-platform application I have started to use Boost, but I can't understand how I can implement code to reproduce behavior of Win32's critical section or .Net's lock.

I want to write a method Foo that can be called from different threads to control write operations to shared fields. Recursive calls within the same thread should be allowed (Foo() -> Foo()).

In C# this implementation is very simple:

object _synch = new object();
void Foo()
{
    lock (_synch)  // one thread can't be lock by him self, but another threads must wait untill
    {
        // do some works
        if (...) 
        {
           Foo();
        }
    }
}
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Vie
  • 823
  • 1
  • 10
  • 18

2 Answers2

11

With boost you can use boost::lock_guard<> class:

class test
{
public:
 void testMethod()
 {
  // this section is not locked
  {
   boost::lock_guard<boost::recursive_mutex> lock(m_guard);
   // this section is locked
  }
  // this section is not locked
 }
private:
    boost::recursive_mutex m_guard;
};

PS These classes located in Boost.Thread library.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • 1
    Your recommendation is very useful. I have fixed my error, BUT i have performance suppression! more then 3 times! =( (before that fix i used unique_lock) – Vie Aug 17 '11 at 14:30
  • 1
    Can you provide code samples where you using shared resources? Also check this http://home.roadrunner.com/~hinnant/mutexes/locking.html#Shared. Maybe it will be useful. – Alexander Verbitsky Aug 18 '11 at 08:17
  • 1
    Try to avoid using recursive locking, may be by moving guard to outer scope. Recursive mutexes can be slower then simple ones. – blaze Feb 16 '12 at 08:19
3

Here's a rewrite of your example, using Boost.Thread: I removed the comments, but otherwise, it should be a 1-to-1 rewrite.

boost::recursive_mutex mtx;

void Foo()
{
    boost::lock_guard<boost::recursive_mutex> lock(mtx);
    if (...) 
    {
       Foo();
    }
}

The documentation can be found here.

Note that Boost defines a number of different mutex types. Because your example shows the lock being taken recursively, we need to use at least boost::recursive_mutex.

There are also different types of locks. In particular, if you want a reader-writer lock (so that multiple readers can hold the lock simultaneously, as long as no writer has the lock), you can use boost::shared_lock instead of lock_guard.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
jalf
  • 243,077
  • 51
  • 345
  • 550