You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<iostream>
#include<vector>
#include<thread>intmain(int argc, char* argv[]) {
std::vector<std::thread> v;
for (int i = 0; i < 3; i++) {
v.push_back(std::thread([] {
// Do some work inside the thread
}));
}
for (std::thread& thread : v) {
thread.join();
}
for (std::thread& thread : v) {
thread.detach();
}
return0;
}
std::thread inside a loop using emplace_back
#include<iostream>
#include<vector>
#include<thread>intmain(int argc, char* argv[]) {
std::vector<std::thread> v;
for (int i = 0; i < 3; i++) {
v.emplace_back([] {
// Do some work inside the thread
});
}
for (std::thread& thread : v) {
thread.join();
}
return0;
}
non-copyable
classNonCopyable {
public:NonCopyable() = default;
~NonCopyable() = default;
// Delete the copy constructor and copy assignment operatorNonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
// Other member functionsvoiddoSomething() {
// Code for the member function
}
};