Boost logo

Boost Users :

From: Bruno Martínez (br1_at_[hidden])
Date: 2006-06-09 23:51:28


On Fri, 09 Jun 2006 22:51:48 -0300, Peteris Krumins [Newsgroups]
<pknewsgroups_at_[hidden]> wrote:

>
> Hello!
>
> I am using a library which itself internally does not use any thread
> sync mechanisms but it provides callback functions
> lib_lock() and lib_unlock() which I can specify if the lib is used in
> multithread environment.
> lib_lock() would get called before critical segment and after it
> lib_unlock() would get called.
>
> I read Boost.Thread documentation and I understand that lock concept is
> not thread-safe itself and should be used only at block scope.
> This makes me think that I have no way to use locking provided by Boost
> to acquire a lock in
> lib_lock() function and release it in lib_unlock()? But I am not sure.
>
> Do you have any suggestions if this can be done using Boost?

You can protext the lock with another, short-lived, lock:

mutex m1;
mutex m2;
mutex::lock g_lk(m2, false);

lib_lock()
{
   mutex::lock lk(m1);
   g_lk.lock();
}

lib_unlock()
{
   mutex::lock lk(m1);
   g_lk.unlock();
}

This isn't 100% OK because the lock is used from different threads, but
should work. In any case, you can write a mutex with the monitor pattern,
but that's even uglier.

You should bug the Boost.Threads maintainers for a member::unsafe_lock()
method.

Bruno


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net