
----- Original Message -----
From: "Christian Ost"
Hi,
I have a question concerning the removal from entries of a thread_group. Consider the following code (i am working on a small threaded server):
in TCPServer.h:
boost::thread_group threads
in TCPServer.cpp:
int TCPServer::loop(int listen_fd) { for (;;) { [connect client] boost::thread* thrd = new boost::thread(boost::bind(&TCPServer::readMessages, this, rfd)); threads.add_thread(thrd); } } }
int TCPServer::readMessages(int rfd) { boost::this_thread::at_thread_exit(boost::bind(&TCPServer::exitThread, this)); for(;;) { [read message] if ([buffer is empty]) { [close connection] return(0); } } return 1; }
void TCPServer::exitThread() { cout << "thread exited" << endl; }
Now I would like to remove the exited thread from the thread_group, however to do this I would need its thread* object - but the maximum of information I can get from boost::this_thread is a boost::thread::id and I haven't found a possibility to get a thread by its thread::id so far.
How can/should I solve this issue? I would be quite happy about any advice...
Hi, I suppose that you have find a solution. Anyway here they are some hits: you can pass the thread* tp and the thread_group to the thread function shared_ptr<thread> tp; tp.reset(threads.create_thread(boost::bind(&TCPServer::readMessages, this, rfd, tp, threads))); int TCPServer::readMessages(int rfd, shared_ptr<thread>th_ptr, thread_group& tg) { //.. tg.remove_thread(th_ptr.get()); //... } This is safe because the thread_group class is thread _safe. If the thread_group stores the thread::id instead of the thread pointer, it can provide a function remove_thread(thread:id); Given a thread* we can alway get the thread::id but the contrary is false. The thread_group class is not to big. You can adapt it waiting for the feature to be in threads. Best, Vicente