Boost logo

Boost Users :

Subject: Re: [Boost-users] Beginning with thread
From: Benjamin Sobotta (benjamin.sobotta_at_[hidden])
Date: 2010-08-13 09:21:29


On Friday 13 August 2010 13:46, Kraus Philipp wrote:
> Hi,
>
> I would like to use Boost threads, but I don't work with them. I know
> only Java Threads.
>
> The problem, in which should use threads:
> I have some stochastic and numerical optimization, which all can
> calculate independent (no mutex, synchronizsation or something else).
>
> I've found boost::thread and boost::bind for creating threads (call a
> method of an object).
>
> In Java I would write this code structure:
>
> myThread first = new myThread();
> first.setParameter1(....)
> first.setParameter1(....)
>
> Thread th = new Thread(first);
> th.start();
>
> first.getData()
>
>
> I would like to have an object of calculation, which receives only
> after creating different parameters and then calculates the data on
> more than one thread.
> After completion of the calculation, I can let me deliver the data
> back from any object, like:
>
> std::vector<myWorker> x;
> for(int i=0; i < numThreads; ++i) {
> x.push_back( myWorker() );
> x[i].setParameter(....);
> }
>
> createThreadGroup (x);
> wait until ThreadGroup is finish
>
> for(int i=0; i < numThreads; ++i)
> x[i].getData()
>
> How can do this with Boost Thread? Is this the correct way or can I do
> this more efficient?
>
> Thanks
>
> Phil

Hi!

Ok if I understand this right you want one thread per worker object?! I would
do something like

std::vector<myWorker> x;
for(int i=0; i < numThreads; ++i) {
       x.push_back( myWorker() );
       x[i].setParameter(....);
}

boost::thread_group threads;

for(int i=0; i < numThreads; ++i)
{
        threads.create_thread(boost::bind(&myWorker::compute_stuff), x[i]);
}

threads.join_all();

// retrieve information from the workers

That would send a thread into each myWorker with entry point compute_stuff().
At least in theory. Right now I've no way / time of testing a setup like
this.

Ben


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