Creating threads in a loop

I just tested something like this:

boost::thread workerThread1(boost::bind(&Class::Function, this, ...); boost::thread workerThread2(boost::bind(&Class::Function, this, ...); 

and it works great. Now I want to create as many threads as I have objects in the list. I have an experiment with boost :: foreach and it works fine. But I have problems with stream names.

Thus, the simplified code is as follows:

 for { boost:thread name(...); } 

but, of course, the name cannot be here in the loop because it rewrites itself and is not available after the loop. How to create threads so that I can combine them all after creation?

+4
source share
5 answers

Can't you create a list (or similar) of threads, and then just create them and add to the list.

Something like the following (which is most likely more pseudo-code anything :-))

 list<boost::thread*> threads; for { boost::thread* name = new boost::thread(...); threads.push_back(name); } 

As mentioned in another answer, you can use smart pointers that would be better, and you mentioned that you have a certain number of threads, so an array / vector would be a better choice, but as I said, the code above is not perfect in anyway

+3
source

Why aren't you using boost::thread_group ? You can create / add / delete threads and join them ( boost::thread_group::join_all() ).

 boost::thread_group tgroup; for(...) { tgroup.create_thread(boost::bind(&Class::Function, this, ...)) ; } tgroup.join_all(); 

But be careful with how many threads you create, too many threads can lead to OutOfMemory .

+12
source

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.

+3
source

Why not put the streams in your own container, for example a vector (by a smart pointer, provided that they are not copied)?

+2
source

Disclaimer: I do not use boost, but if it works like the rest of C ++, I believe that it could be on the right line. Will remove if it is trash.

 boost::thread** threads; threads = new boost::thread*[THREAD_COUNT]; for(int i = 0; i < THREAD_COUNT; i++) { threads[i] = new boost::thread(...); } ... for(int i = 0; i < THREAD_COUNT; i++) delete threads[i]; delete[] threads; 

...

The idea is to dynamically allocate an array of pointers to your type of object based on how much you want. Then, for each of them, dynamically create one and call the corresponding constructor in a loop. In the end, you'll need to clear them so you can use the delete [] calls. I do not understand why malloc () / free () and / or vector types will not work either.

0
source

Source: https://habr.com/ru/post/1389481/


All Articles