Guest User

Untitled

a guest
Mar 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #include <cstdio>
  2. #include <thread>
  3. #include <iostream>
  4. #include <chrono>
  5. using namespace std;
  6. void doSomething(int id) {
  7. //время ожидания в милисекундах
  8. std::this_thread::sleep_for(std::chrono::milliseconds(2000));
  9. cout << "Thread id = " << id << endl;
  10. }
  11. void spawnThreads(int n) {
  12. thread threads[n];
  13. //Создаем n объектов "потоков":
  14. for (int i = 0; i < n; i++) {
  15. threads[i] = thread(doSomething, i + 1);
  16. }
  17. // Запускаем их и ждем завершения каждого (присредияем главный поток к побочным)
  18. for (auto& th : threads) {
  19. th.join();
  20. }
  21. }
  22. int main() {
  23. while(true){
  24. spawnThreads(5);
  25. //ввдите "x" (англ), чтобы выйти.
  26. if(getchar() == 'x')
  27. return 0;
  28. }
  29. return 0;
  30. }
Add Comment
Please, Sign In to add comment