Boost logo

Boost :

From: Anthony Williams (anthony.williamsNOSPAM_at_[hidden])
Date: 2003-10-24 03:31:39


"Hurd, Matthew" <hurdm_at_[hidden]> writes:
> A further note: The exception characteristics for a message queue are
> particularly interesting. Normally, as per exceptional c++, you'd separate
> the top and pop part, or head and remove_head, for the reasons discussed in
> Sutter's book. However, in a threaded case, you want this as an atomic op
> and you have an exception problem AFAICT. Not sure what the answer is here
> with these competing concerns. Any thoughts?

Have the queue return an object that locks the queue for the duration of its
existence, which contains functions for top and pop.

e.g.

template<typename T>
class MessageQueue
{
public:
    class MessageQueueQueryObject: boost::noncopyable
    {
    public:
        T top() const;
        void pop();
    };

    std::auto_ptr<MessageQueueQueryObject> query();
};

MessageQueue<std::string> queue;
std::string var;
{
    std::auto_ptr<MessageQueueQueryObject> queryObj(queue.query());
    var=queryObj->top();
    queryObj->pop();
}

Anthony

-- 
Anthony Williams
Senior Software Engineer, Beran Instruments Ltd.
Remove NOSPAM when replying, for timely response.

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