|
Boost Users : |
Subject: Re: [Boost-users] thread_group / removal of items
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-12-13 08:01:55
----- Original Message -----
From: "Christian Ost" <christian.ost_at_[hidden]>
To: <boost-users_at_[hidden]>
Sent: Wednesday, August 27, 2008 6:06 PM
Subject: [Boost-users] thread_group / removal of items
>
> 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
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