Boost logo

Boost Users :

From: Rene Rivera (grafik.list_at_[hidden])
Date: 2004-06-13 11:58:41


Tristan King wrote:
> Rene Rivera wrote:
>
>> Or just don't use boost:ref, and have the thread function objects copied.
>>
> Rene, is that possible with a stuct?

Yes, as long as it has default and copy ctors (or usually the auto
generated ones).

> the problem is that i need give the thread some initial parameters, and
> i'm not sure if thats possible using a simple function.

The most elegant solution is to use Boost.Bind. For example your
original code could be written as such:

#include <iostream>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>

void simple_thread(int i)
{
     std::cout << i << std::endl;
}

int main()
{
   boost::thread_group threads;

   for ( int i = 0 ; i < 5 ; ++i )
   {
       threads.create_thread( boost::bind(&simple_thread,i) );
   };

   threads.join_all( );

   return 0;
}

> if a thread is created using new
> simple_thread* trd = new simple_thread( p );
> and started using:
> threads.create_thread( boost::ref( *trd ) );
> trd still needs to be destroyed using "delete" somewhere right?

Yes.

> or will the memory be free'd when the thread finishes execution?

No.

HTH.

-- 
-- Grafik - Don't Assume Anything
-- Redshift Software, Inc. - http://redshift-software.com
-- rrivera/acm.org - grafik/redshift-software.com - 102708583/icq

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