I am building an application based on an example on the boost website. These are the relevant definitions to know of:
typedef boost::shared_ptr< connection > connection_ptr; std::set< connection_ptr> connections_; std::vector< boost::shared_ptr< boost::thread> > threads;
where connection is a class.
In one of the files, connection_manager.cpp, they are doing like this:
void connection_manager::start(connection_ptr c) { connections_.insert(c); c->start(); }
Now, I want to start every new connection in a separate thread due to my program structure. So I have modified the above according to:
void connection_manager::start(connection_ptr c) { boost::shared_ptr< boost::thread > thread(new boost::thread( boost::bind(&connection::start, c))); // push the newely created thread into the vector threads.push_back(thread); // save the connection in our set connections_.insert(c); }
My problem, and hence this question, is when I want to only one of these connection_ objects. In the link provided earlier, they do like this:
void connection_manager::stop(connection_ptr c) { connections_.erase(c); c->stop(); // find the connection in among the threads and join that thread }
But as the comment above suggests, how do I find c amongst all the threads and stop only that thread. I want to call the join() function for that thread.
Update:
I think that this is actually what I really want to have! So I declare my variable as
std::map < connection_ptr, boost::shared_ptr < boost::thread > > threads;
But then, how do I create a new tread, same way as before? Like:
boost::shared_ptr < boost::thread > thread(new boost::thread( boost::bind(&connection::start, c)));
But what is then the next step? Sorry for being confused... :-)