====== Синхронизация потоков через scoped_lock ====== // Example program #include #include #include #include #include #include #include using namespace std; std::string format(const char *fmt, ...); void start(const int f, const int t, vector* vec); std::mutex vecmgr; int main() { vector vec; start(20, 40, &vec); start(100, 120, &vec); start(200, 240, &vec); string name; cout << "What is your name? "; getline(cin, name); cout << "Hello, " << name << "!\n"; for (auto s: vec) { cout << s << ", "; } cout << endl; } void start(int f, int t, vector* vec) { auto thfunc = [&f, &t, vec] () { // scoped_lock lock{vecmgr}; if (f > t) return; for (int i=f; i<=t; i++) { vec->push_back( i ); } }; thread th(thfunc); th.join(); // th.detach(); }