You can save them in an array:
size_t const thread_count = 5; boost::thread threads[thread_count]; for (size_t i = 0; i < thread_count; ++i) { threads[i] = boost::bind(&Class::Function, this, ...)); }
In C ++ 11, you can save std::thread in friendlier containers like std::vector :
std::vector<std::thread> threads; for (int i = 0; i < 5; ++i) { threads.push_back(std::thread(boost::bind(&Class::Function, this, ...)))); }
This will not work with boost::thread in C ++ 03, since boost::thread not copied; a temporary assignment in my example works because of some Boost magic that sorts emulates the semantics of movement. I also could not get it to work with boost::thread in C ++ 11, but it could be because I don't have the latest version of Boost. So in C ++ 03 you are stuck with either an array or a container of (preferably smart) pointers.
source share