468,513 Members | 1,799 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Passing a parameter to a thread (boost)

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
Dec 2 '07 #1
2 3425
JackC wrote:
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.
Dec 2 '07 #2
On Dec 2, 11:15 pm, JackC <jeche...@gmail.comwrote:
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).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 3 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by AMS | last post: by
2 posts views Thread by muser | last post: by
7 posts views Thread by Harolds | last post: by
6 posts views Thread by Catherine Jones | last post: by
4 posts views Thread by Lighter | last post: by
3 posts views Thread by Gary Wessle | last post: by
4 posts views Thread by Gary Wessle | last post: by
8 posts views Thread by asit | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.