Boost logo

Boost Users :

Subject: [Boost-users] asio - improving chat server example for my usage
From: john grant (johnkonradgrant_at_[hidden])
Date: 2012-05-18 15:30:45


Hi,I could use some help to build on Asio's chat_server example so I can use it in my app. I want the server to have a main thread loop, like you would need if the server had a user interface or some other responsibility than just accepting client connections and echo-ing their input. I added a thread_group to call 'io_service::run' (to allow for asynchronous connections), letting the server have a main app loop for other responsibilities. I've learned enough about Asio by watching and reading Michael Caisse's excellent Asio presentation from BoostCon 2010 to feel somewhat comfortable using Asio and Threads together. I have a client app that connects well with the server app. ** I have a problem with my server app upon shutdown. The problem is that the thread_group I am using is stuck at the 'join_all' call (in the server app class's destructor). ** 1- I was worried that the threads were all dead, so I added a work object to my server app class, and now I delete it before I call 'join_all', but it still hangs here. 2- This might be way off, but I also wondered if I need to close all the sockets so that the thread group could join. I added 2 member functions to help me: void chat_server::close() {   room_.close(); } void chat_room::close() {   std::for_each( participants_.begin(), participants_.end(), boost::bind(&chat_room::leave, this, _1) ); } I call this in my server app class's destructor before calling thread_group::join_all, but this has a crash. The crash occurs right after chat_room::close() completes. I'm not confident in the way I used bind(...), but I was able to see the function execute, and then the crash occurs later. ** Do you know what I am doing wrong that causes the thread_group to hang? If I need to close the sockets, can you show me how to do that so I do not crash my app? ** For additional context, my server needs to send vehicle state to clients, among other messages. The server has a real time physics simulation. My client app will show a representation of the physics simulation. Many thanks for any help. I'm really enjoying Asio and other parts of Boost, but I still don't understand the framework completely. By the way, the IRC channel has been a big help already, but it seems everyone is busy in Aspen. :) John



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net