Asio and bind. Queuing a bind call.
Hi All. I'm putting together a class which provides a single point of contact to a resource. I'm planning on using the asio IO service in conjunction with a ASIO strand to control flow of method calls to my resource. I've got this working and ASIO has made this so easy. One of the things I've got to do is to be able to pause and queue function calls to the resource if the resource can't service the request. I figured the best way of doing this was putting the result of boost::bind in to some sort of queue when the resource is unavailable and then when it becomes available processing the items in the queue. Is this the best way to do it or does the strand or IO service have a way of doing it? As the result of boost::bind is a template I don't know of an stl or boost container which will store these objects. As there are a number of functions that can be called on my resource with differing arguments I don't think I can use a boost::function as I don't know the arguments to my generic post call. Here's some sudo code which may help to explain it. class CResourceManager { public: // post. // this function takes in a bound function and addes it to the queue of functions to call. // it reserves the sequence of calls by using the io strand. // this means that any function called by this post call is guaranteed to be called in sequence and will be the only function called at that time. template <typename CompletionHandler> void Post(CompletionHandler handler) { if (m_bStrandEnabled) { m_strand.post( handler ); } else { // strand is not enabled. ?????? m_queuedCalls.push( handler ); ????? } } void EnableStrand() { iterate through queue posting functions on to the strand. ?????? code would depend on container used. m_bStrandEnabled = true; } boost::asio::io_service &m_ioService; boost::asio::io_service::strand m_strand; private: bool m_bStrandEnabled; // as a boost bind function is templated we have to work out a way of putting an unknown template in to the queue. queuedfuncitoncontainer m_queuedCalls; } at the moment I don't know what type the "queuedfuncitoncontainer" should be. Because the result of boost::bind is dependent on the function being bound it doesn't seem like I can use stl::queue. Anyone have any ideas how I can do this? I've done a bit of googling and didn't really get any ware. I did look at the boost code to see how strand does it and it seems like its using raw pointers, I didn't really want to go down that road if I can help it. Thanks. Nick. Nick Adamson Software Engineer Advanced Projects and Technology General Dynamics United Kingdom Limited Bryn Brithdir, Oakdale Business Park, Blackwood, South Wales, NP12 4AA Telephone: +44 (0)1495 23 6467 Email: Nick.Adamson@generaldynamics.uk.com Website: www.generaldynamics.uk.com http://www.generaldynamics.uk.com/ To help reduce waste paper, please only print this email if you really need to.
On Fri, Nov 5, 2010 at 7:08 AM,
container which will store these objects. As there are a number of functions that can be called on my resource with differing arguments I don't think I can use a boost::function as I don't know the arguments to my generic post call.
I think boost::function
Hi Nat.
Thanks. That works nicely!
Thanks.
Nick.
-----Original Message-----
From: boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Nat Linden
Sent: 05 November 2010 16:15
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] Asio and bind. Queuing a bind call.
On Fri, Nov 5, 2010 at 7:08 AM,
participants (2)
-
Nat Linden
-
Nick.Adamson@generaldynamics.uk.com