My code looks like the following:
#include <list>
#include <thread>
void my_function(int val) {
// Empty function
}
int main() {
std::list<std::thread> threads;
for (int i = 0 ; i < 10 ; i++) {
threads.push_back(std::thread(my_function, i));
}
return 0;
}
The fact that I use threads.push_back()
means that I run the copy-constructor std::thread::thread(const thread&)
.
Is it safe?
Should I use
std::move
?
Please suppose that I don't know in advance how many threads I am going to need, so replacing the list by an array or by an std::vector
, is not an option for me (std::vector
would be an option only if I knew the number of threads in advance, as I cannot afford the vector's realloc
operations).