开发者

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;
}


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.


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.
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜