Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2003-11-05 09:19:57


Daniel Spangenberg wrote:
>
> Alexander Terekhov schrieb:
>
> > Daniel Spangenberg wrote:
> > [...]
> > > no, it doesn't. The problem is not, that I could not find any workaround for that
> > > and I am also not preaching to introduce a copy semantic for those objects.
> >
> > But you should try. It might help you to solve your puzzle (simple
> > rename()/swap_names() aside for the moment).
> >
> > http://www.boost.org/libs/thread/doc/faq.html#question5
> > (std::less() aside for a moment)
> >
>
> I am aware of this example, but it does not show a reasonable definition of a member
> swap function ...

Uhmm, "untested" (see also www.boost.org/libs/thread/example/condition.cpp***):

void bounded_container::swap(bounded_container & other) { // strong (see blow)

  condition * broadcast_not_full = 0,
            * broadcast_not_empty = 0;

  if (this != &other) {

    bool this_is_less = std::less<bounded_container *>()(this, &other);

    // In theory, initial calls may actually throw here... (QoI)
    // in spite of non-static init. See 3FA61F96.223FDE85_at_[hidden]
    mutex::guard guard1(this_is_less ? m_mutex : other.m_mutex);
    mutex::guard guard2(this_is_less ? other.m_mutex : m_mutex);

    if (m_container.empty() != other.m_container.empty())
      broadcast_not_empty = m_container.empty() ?
        &m_not_empty : &other.m_not_empty;

    if (m_container.full() != other.m_container.full())
      broadcast_not_full = m_container.full() ?
        &m_not_full : &other.m_not_full;

    m_container.swap(other.m_container); // must be strong or nothrow

  }

  if (broadcast_not_full)
    broadcast_not_full->broadcast(); // must be nothrow

  if (broadcast_not_empty)
    broadcast_not_empty->broadcast(); // must be nothrow
}

Oder?

regards,
alexander.

***) It's always better to signal condvars NOT holding the mutex, BTW.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk