How to know if a asio thread exit and how to restart a new one?

1,233 views
Skip to first unread message

Jerry Yin

unread,
Jul 24, 2013, 4:44:56 AM7/24/13
to webso...@googlegroups.com
Hi,

Are there any way to restart a asio thread if the old one has exit? My use case is like below:

//
connect
close

..do something.

connect
close
//

After the first close the asio thread will exit, I want to create a new asio thread if I found that the asio thread has exit.
My question is:
1. How to know if a asio thread is stopped? Are there any way to check the client object to see if all connections are closed?
2. After created the new asio thread, besides client.reset(), any other method I need to call to make sure the client object is in a correct status?

Thanks.



Jerry Yin

unread,
Jul 26, 2013, 11:23:05 AM7/26/13
to webso...@googlegroups.com
I think I found the answer by myself. This is a boost asio question. we can call the io_service::stopped() to check if the io_service is stopped.
use Client.get_io_service().stopped() to check if we need to create a new io_service thread.

Peter Thorson

unread,
Jul 26, 2013, 11:55:59 AM7/26/13
to Jerry Yin, webso...@googlegroups.com
The short answer here is that for the iostream transport all of the run loop functionality is a stock boost asio io_service. You can look at the boost asio documentation for more detail about how it works. WebSocket++ endpoints using the asio transport wrap a number of the more commonly used io_service methods for convenience. You can also access the io_service directly or use an external io_service that you manage completely.

Some notes / tips about io_service use in WebSocket++
- stopped() will test whether or not the run loop has stopped / run out of work
- stop() will stop a running endpoint
- If an endpoint is stopped then all connections are/have been forcibly/uncleanly closed
- reset() will reset a stopped io_service and get it ready to call run() again. There is no other endpoint state that needs to be changed or reset.
- after calling reset() and before calling run() you will need to push at least one WebSocket operation into the endpoint. For servers this is usually listen(), for clients connect(). If you don't, run() will return immediately because it has nothing to do.
- Servers will listen forever. As of version 0.3.0-alpha3 there is no way to stop listening in a graceful way. This is functionality that I intend to add but haven't gotten to yet.
- There is no built in way to request that all connections cleanly close, you may keep a list of all connections and call endpoint::close on each of them and wait until all close handlers have been called. I have not decided whether this functionality should be provided by the library. In order to do so the library would need to keep a centrally managed data structure of all connections. This has performance and thread safety implications.
- You may add your own non-WebSocket++ tasks to the io_service.

For the workflow that you describe below you can either call reset(), connect(), then run() after each time or you can use another technique that keeps the io_service running even if it has no work to do. See the following article from the asio documentation for more details.: 


At some point this functionality may be added to the library, but for now it can easily be done manually.

Peter

--
You received this message because you are subscribed to the Google Groups "WebSocket++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to websocketpp...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted

Jerry Yin

unread,
Jul 28, 2013, 10:57:43 PM7/28/13
to webso...@googlegroups.com, Jerry Yin
Thank you peter, your answer gives me a thorough solution on my case.
Reply all
Reply to author
Forward
0 new messages