So we have class with functions a and b. Thread one calls a and no other thread can call a or b untill one would call b. Meaning thread one would be capable to call a and than a and … and than a, and while one had not called b other threads that want to call a or b stand waiting. is it possible to do such thing with boost::mutex and how to do it?
So we have class with functions a and b . Thread one calls a
Share
The mutex is not a problem; it’s the lock. The simplest solution is
just to call
mutex::lock()andmutex::unlock()manually, and forgetabout the
mutex::scoped_lock; after all, you don’t want the lock torespect scope. The problem with this is the usual one; you probably
want to free the lock in case of an exception. One solution would be to
allocate the
mutex::scoped_lockdynamically, and use astd::auto_ptror a
boost::shared_ptrto manage it. (Curiously enough, neitherboost::mutex::scoped_locknorstd::lock_guardare movable, so youneed dynamic allocation in order to transfer ownership.)