Yochai Timmer
Yochai Timmer

Reputation: 49221

Boost - shared_mutex has to be same instance?

I want to use shared_mutex with shared/unique locks for read/write.

Now if I have 2 objects and i want them to use the same lock, can I assign the value of the first mutex to the second mutex ?
Or do I have to work create a pointer to the shared_mutex and then have them both point to the same object instance?

I mean, will this work correctly, and both objects will work on same lock ?:

typedef boost::shared_mutex ReadWriteMutex;
    class A {
    ReadWriteMutex lock;
}

void test() {
    A a = new A();
    B b = new B()
    b.lock = a.lock;
}

Upvotes: 1

Views: 344

Answers (2)

Null Set
Null Set

Reputation: 5414

This will not work correctly. shared_mutex derives from boost::noncopyable. What you want to use instead is a pointer or reference to the mutex.

Upvotes: 4

Loki Astari
Loki Astari

Reputation: 264331

I would rather create the lock seprately then pass it to your objects.

void test()
{
    ReadWriteMutex  lock;
    A a(lock);             // Notice there is no new here.
    A b(lock);

    // DO Stuff with a and b.
}

Upvotes: 2

Related Questions