Re: [Boost-users] [asio] how to specify a timeout?

Use an asio::deadline_timer Example: asio::io_service m_ioService; asio::deadline_timer * m_timer; m_timer = new asio::deadline_timer(m_ioService); m_timer->expires_from_now( boost::posix_time::seconds(t) ); m_timer->async_wait( boost::bind(&Foo:handle_timeout, this, asio::placeholders::error) ); asio::async_read( *m_socket, asio::buffer( m_msg.header(), m_msg.header_length() ), boost::bind(&Foo::handle_read_header, this, asio::placeholders::error) ); This is a cut and paste from my code, if it isn't so clear I'll explain deeply. Tks ----- Messaggio originale ----- Da: Arkadiy Vertleyb <vertleyb@hotmail.com> A: boost-users@lists.boost.org Inviato: Lunedì 3 dicembre 2007, 20:05:15 Oggetto: [Boost-users] [asio] how to specify a timeout? Hi all, Is it possible in asio to supply a timeout for a read/write operation? Thanks, Arkadiy _________________________________________________________________ You keep typing, we keep giving. Download Messenger and join the i’m Initiative now. http://im.live.com/messenger/im/home/?source=TAGLM _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users ___________________________________ L'email della prossima generazione? Puoi averla con la nuova Yahoo! Mail: http://it.docs.yahoo.com/nowyoucan.html

Hello Andrea, "Andrea Bartoli" <bartols2000@yahoo.it> wrote
Use an asio::deadline_timer
Example:
asio::io_service m_ioService; asio::deadline_timer * m_timer; m_timer = new asio::deadline_timer(m_ioService);
m_timer->expires_from_now( boost::posix_time::seconds(t) ); m_timer->async_wait( boost::bind(&Foo:handle_timeout, this, asio::placeholders::error) ); asio::async_read( *m_socket, asio::buffer( m_msg.header(), m_msg.header_length() ), boost::bind(&Foo::handle_read_header, this, asio::placeholders::error) );
Thanks for your response. Still, it's not clear to me how to use it. In your example it seems that read operation and timeout are two independent things. How do I make sure only one of these two fires? Is it possile to cancel read operation once timout has been recieved? Thanks, Arkadiy

Hi, Can you explain Is it posibly to use timeouts if multiple threads are calling io_service::run() on the single io_service object? Thanks On Tuesday 04 December 2007 12:19:36 Andrea Bartoli wrote:
Use an asio::deadline_timer
Example:
asio::io_service m_ioService; asio::deadline_timer * m_timer; m_timer = new asio::deadline_timer(m_ioService);
m_timer->expires_from_now( boost::posix_time::seconds(t) ); m_timer->async_wait( boost::bind(&Foo:handle_timeout, this, asio::placeholders::error) ); asio::async_read( *m_socket, asio::buffer( m_msg.header(), m_msg.header_length() ), boost::bind(&Foo::handle_read_header, this, asio::placeholders::error) );
This is a cut and paste from my code, if it isn't so clear I'll explain deeply. Tks -- Wbr, Nikita

"Nikita Bige" <big-nikita@yandex.ru> wrote
Can you explain Is it posibly to use timeouts if multiple threads are calling io_service::run() on the single io_service object?
I think this is much simpler... I start an asynchronous read giving it a function object to call when it is done. However, if it is not done in let's say 1 sec, I want to _cancel this read_ and take an alternative route. I don't want the read function object to be called if the read completes _after_ the timeout has been processed. Isn't this a common scenario? Thanks, Arkadiy
participants (3)
-
Andrea Bartoli
-
Arkadiy Vertleyb
-
Nikita Bige