[Boost-users] [asio] How to know if the io_service is runing

174 views
Skip to first unread message

Daniele Barzotti

unread,
Sep 24, 2009, 4:24:14 AM9/24/09
to Boost Users
In my library I have this code (yet used in another project) where pwork
is assigned to a shared_ptr:

--------------------------------------------------------------
typedef boost::shared_ptr<boost::asio::io_service::work> io_work_ptr;

// Main io_service
static boost::asio::io_service io_service;
static boost::scoped_ptr<boost::thread> io_service_thread;
static boost::asio::io_service::work* p_work;
static bool thread_started;


bool CreateMainThread()
{
if (!thread_started) {
try {
// create the work object on the heap
p_work = new boost::asio::io_service::work(io_service);

// run the IO service as a separate thread:
io_service_thread.reset (
new boost::thread (
boost::bind (
&boost::asio::io_service::run, &io_service
)
)
);

thread_started = !thread_started;
return true;

} catch (boost::thread_resource_error e) {
// Failed to create the new thread
return false;
}
}
}
--------------------------------------------------------------

WaveStream::WaveStream() : rx_timer_(io_service)
{
};

WaveStream::Test()
{
//Start a new timer or renew it
rx_timer_.expires_from_now( boost::posix_time::milliseconds(500) );
// We managed to cancel the timer. Start new asynchronous wait.
rx_timer_.async_wait(
boost::bind(&WaveStream::handle_timeout, this, _1)
);
}

void WaveStream::handle_timeout(const boost::system::error_code& error)
{
....
}
--------------------------------------------------------------

The timer callback is never called.
Is there a way to know if the io_service is runing?

Daniele.


_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Patryk Bukowinski

unread,
Sep 24, 2009, 4:38:06 AM9/24/09
to boost...@lists.boost.org
Hi,

2009/9/24 Daniele Barzotti <daniele....@eurocomtel.com>
I'm thinking would that be correct if you call timer after async_wait() ?
 
 // We managed to cancel the timer. Start new asynchronous wait.

have You tried restet io_service in this thread, before putting the query on the queue?
io_service.reset()
If thats correct from Your businesses logic.
 rx_timer_.async_wait(
   boost::bind(&WaveStream::handle_timeout, this, _1)
 );
}

void WaveStream::handle_timeout(const boost::system::error_code& error)
{
 ....
}
--------------------------------------------------------------

The timer callback is never called.
Is there a way to know if the io_service is runing?

Daniele.


_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users


cheers
P.

Daniele Barzotti

unread,
Oct 14, 2009, 5:17:15 AM10/14/09
to Boost Users
Daniele Barzotti ha scritto:

> In my library I have this code (yet used in another project) where pwork
> is assigned to a shared_ptr:
> .....

> The timer callback is never called.

I've change the code in this way:


> --------------------------------------------------------------
> typedef boost::shared_ptr<boost::asio::io_service::work> io_work_ptr;
>
> // Main io_service
> static boost::asio::io_service io_service;

> static boost::asio::io_service::work* p_work;

The io_service::work now is stored into a shared_ptr:

static io_work_ptr p_work;

so I'm pretty sure it is running.

> bool CreateMainThread()
> {
> if (!thread_started) {
> try {
> // create the work object on the heap
> p_work = new boost::asio::io_service::work(io_service);

p_work.reset( new boost::asio::io_service::work(io_service) );


--------------------------------------------------------------
WaveStream::WaveStream() : rx_timer_(io_service)
{
};

WaveStream::Test()
{
io_service.reset();


//Start a new timer or renew it
rx_timer_.expires_from_now( boost::posix_time::milliseconds(500) );

//Start new asynchronous wait.


rx_timer_.async_wait(
boost::bind(&WaveStream::handle_timeout, this, _1)
);
}

void WaveStream::handle_timeout(const boost::system::error_code& error)
{
....
}
--------------------------------------------------------------

I don't know why the handler is not called!

Reply all
Reply to author
Forward
0 new messages