How to manually lock and unlock a mutex?

Hi there, I'm converting over our codebase to make use of the boost libs. Is there a way to manually lock and unlock a boost:mutex? Or, is there a conditional lock available? I know there is boost::mutex::scoped_lock that does it automatically. Here is code example that, I think, requires manual locking: void foo() { if( condition ) oMutex.lock; do something if( condition ) oMutex.unlock; } It might be that our code isn't designed very well for using boost::mutex. One solution would be: void foo() { if( condition ) { boost::mutex::scoped oLock( oMutex ); do something } else { do something } } Thanks, Christian

Christian Henning wrote:
Hi there, I'm converting over our codebase to make use of the boost libs. Is there a way to manually lock and unlock a boost:mutex? Or, is there a conditional lock available? I know there is boost::mutex::scoped_lock that does it automatically.
Here is code example that, I think, requires manual locking:
void foo() {
if( condition ) oMutex.lock;
do something
if( condition ) oMutex.unlock;
}
void foo() { mutex::scoped_lock lock( oMutex, condition ); // do something }

Hi Christian, You can do: mutex::scoped_lock lock(m, condition); On 7/29/05, Christian Henning <chhenning@gmail.com> wrote:
Hi there, I'm converting over our codebase to make use of the boost libs. Is there a way to manually lock and unlock a boost:mutex? Or, is there a conditional lock available? I know there is boost::mutex::scoped_lock that does it automatically.
Here is code example that, I think, requires manual locking:
void foo() {
if( condition ) oMutex.lock;
do something
if( condition ) oMutex.unlock;
}
It might be that our code isn't designed very well for using boost::mutex.
One solution would be:
void foo() { if( condition ) { boost::mutex::scoped oLock( oMutex );
do something } else { do something }
}
Thanks, Christian
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Cory Nelson http://www.int64.org

Try void foo() { shared_ptr<scoped_lock> lk; if (condition) lk.reset(new scoped_lock(mutex)); do something } The smart pointer will automatically unlock when it goes out of scope.
participants (4)
-
Christian Henning
-
Cory Nelson
-
Jason Stewart
-
Peter Dimov