fork download
  1. #include<iostream>
  2. #include<thread>
  3. using namespace std;
  4.  
  5. class arrayModifier {
  6. public:
  7.  
  8. void operator()(int a[], int len) {
  9. for (int i = 0; i < len; i++) {
  10. a[i] *= 2;
  11. }
  12. }
  13.  
  14. void invers(int a[], int len) {
  15. for (int i = 0; i < len; i++) {
  16. a[i] *= -1;
  17. }
  18. }
  19. };
  20.  
  21. int main() {
  22. const int length = 5;
  23. int arr[length] = {1, 2, 3, 4, 5};
  24. arrayModifier obj;
  25. cout << "Output the array before threads\n";
  26. for (int i = 0; i < length; i++) {
  27. cout << arr[i] << ' ';
  28. }
  29. // Инициализируется объект функцией
  30. thread arr_thread(obj, arr, length);
  31. // Инициализируется обычным открытым методом
  32. thread arr_thread2(&arrayModifier::invers, &obj, arr, length);
  33. if (arr_thread.joinable()) arr_thread.join();
  34. if (arr_thread2.joinable()) arr_thread2.join();
  35. cout << "\nOutput th array after threads\n";
  36. for (int i = 0; i < length; i++) {
  37. cout << arr[i] << ' ';
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0s 19344KB
stdin
Standard input is empty
stdout
Output the array before threads
1 2 3 4 5 
Output th array after threads
-2 -4 -6 -8 -10