Quantcast

thread_group / removal of items

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

thread_group / removal of items

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...

Thank you very much!


Christian
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: thread_group / removal of items

Ovanes Markarian
Hi,

just implement an internal mapping of thread id to thread pointer. It can be an unordered_map or stl::map, depends on how many threads you are going to manage.

Just a small addition to you code:

On Wed, Aug 27, 2008 at 7:06 PM, Christian Ost <[hidden email]> wrote:
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);

this can be written:
boost::thread* thrd = threads.create_thread(boost::bind(&TCPServer::readMessages, this, rfd));
Here you don't need to call add_thread...


...

Regards,
Ovanes


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: thread_group / removal of items

Vicente Botet
In reply to this post by Christian Ost
----- Original Message -----
From: "Christian Ost" <[hidden email]>
To: <[hidden email]>
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 mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Loading...