मैं इसे ऐसा करने की कोशिश कर रहा हूं कि मैं एक लूप से धागे को कॉल नहीं कर सकता। लेकिन जब मैं इसे चलाने मैं एक रनटाइम त्रुटि मिलती है:यह सरल धागा कोड क्यों विफल रहता है?
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 हाँ –
इसे 'पकड़ फेंक' के साथ जीडीबी के तहत चलाएं। –
@ शार्थ सही है। लेकिन 'shared_ptrs' क्यों? – juanchopanza