Skip to content

Instantly share code, notes, and snippets.

@ucasfl
Last active March 7, 2021 10:02
Show Gist options
  • Save ucasfl/e0a0bda397621120202d4f5a2f16b262 to your computer and use it in GitHub Desktop.
Save ucasfl/e0a0bda397621120202d4f5a2f16b262 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <vector>
class A
{
public:
void func()
{
auto f = [&](int i) { vec[i] = i; };
vec.resize(10);
std::vector<std::thread> ths;
ths.reserve(10);
for (int i = 0; i < 10; ++i)
{
ths.emplace_back(f, i);
}
for (auto & t : ths)
t.join();
}
private:
std::vector<int> vec;
};
int main()
{
A a;
a.func();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment