[cpp-threads] Comments on N2094 MT API proposal

Anthony Williams anthony at justsoftwaresolutions.co.uk
Fri Sep 15 09:21:51 BST 2006


Hi all,

I've been reading the threading-related papers in the pre-Portland mailing
with interest.

One part of Howards paper N2094 that concerns me is the concept of
"convertible shared ownership" of a mutex. Allowing an upgrade from shared
ownership to exclusive ownership without releasing the shared lock is
providing a nice easy way for people to write code that deadlocks. I know that
the blocking operations are not provided precisely for this reason, but even
the non-blocking variants can cause a problem. Consider two threads running
the following function:

void f(mutex& m)
{
    m.lock_sharable();
    std::thread_util::sleep(100); // or some other time-consuming op
    while(!m.try_unlock_sharable_and_lock())
    {
        std::thread_util::yield();
    }
}

If both threads get the sharable lock, neither will be able to upgrade to a
full lock, so they will both loop forever.

I propose that the "convertible shared ownership" concept is dropped. We
already have the "upgradable ownership" concept, which allows one thread to be
upgradable, but share with others that have just "sharable" ownership, until
it actually needs to upgrade to exclusive ownership.

Anthony
-- 
Anthony Williams
Software Developer
Just Software Solutions Ltd
http://www.justsoftwaresolutions.co.uk



More information about the cpp-threads mailing list