Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <thread>
- #include <iostream>
- #include <chrono>
- using namespace std;
- void doSomething(int id) {
- //время ожидания в милисекундах
- std::this_thread::sleep_for(std::chrono::milliseconds(2000));
- cout << "Thread id = " << id << endl;
- }
- void spawnThreads(int n) {
- thread threads[n];
- //Создаем n объектов "потоков":
- for (int i = 0; i < n; i++) {
- threads[i] = thread(doSomething, i + 1);
- }
- // Запускаем их и ждем завершения каждого (присредияем главный поток к побочным)
- for (auto& th : threads) {
- th.join();
- }
- }
- int main() {
- while(true){
- spawnThreads(5);
- //ввдите "x" (англ), чтобы выйти.
- if(getchar() == 'x')
- return 0;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment