Passing a parameter to a thread (boost)

J

JackC

Hi,

I create my threads like this:

for(int j = 0; j < 5; j++)
{
boost::thread *thr = new boost::thread(worker_func);
threads.add_thread(thr);
}

How would i go about passing a parameter into the worker function? If
possible i would like to pass a structure in.

Thanks for any help,

Jack
 
A

Alf P. Steinbach

* JackC:
I create my threads like this:

for(int j = 0; j < 5; j++)
{
boost::thread *thr = new boost::thread(worker_func);
threads.add_thread(thr);
}

How would i go about passing a parameter into the worker function? If
possible i would like to pass a structure in.

Try reading the documentation.

Cheers, & hth.,

- Alf
 
P

Phil Endecott

JackC said:
Hi,

I create my threads like this:

for(int j = 0; j < 5; j++)
{
boost::thread *thr = new boost::thread(worker_func);
threads.add_thread(thr);
}

How would i go about passing a parameter into the worker function? If
possible i would like to pass a structure in.

Use Boost.Bind, or similar.
 
J

James Kanze

I create my threads like this:
for(int j = 0; j < 5; j++)
{
boost::thread *thr = new boost::thread(worker_func);
threads.add_thread(thr);
}
How would i go about passing a parameter into the worker
function? If possible i would like to pass a structure in.

The worker function is a boost::function, so you can pass it
pretty much anything that's copiable. Be aware that it will be
copied, however; if you want to access it later in the parent
thread (say after a join), then you need to pass a pointer (or
maybe a reference).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top