Sto cercando di farlo fare che non posso chiamare thread da un ciclo. Ma quando l'eseguo ottengo un errore di runtime:Perché questo semplice codice di thread ha esito negativo?
terminate called after throwing an instance of 'std::system_error'
what(): Invalid argument Thread #1
#include <iostream>
#include <vector>
#include <memory>
#include <thread>
#include <mutex>
std::mutex m;
static int thread_count;
auto foo = [&] {
std::lock_guard<std::mutex> lock(m);
std::cout << "Thread #" << ++thread_count << std::endl;
};
int main()
{
std::vector<std::shared_ptr<std::thread>>
threads(20, std::make_shared<std::thread>(foo));
for (const auto& th : threads)
th->join();
}
@juanchopanza Sì –
Eseguilo sotto gdb con 'catch throw'. –
@sharth ha ragione. Ma perché il 'shared_ptrs'? – juanchopanza