fork download
  1. #include <iostream>
  2. #include <thread>
  3.  
  4. using namespace std;
  5.  
  6. class A {
  7. public:
  8. A() {cout << "A: " << this << " A::A()" << endl;}
  9. ;A(int) {cout << "A: " << this << " A::A(int)" << endl;};
  10. ~A() {cout << "A: " << this << " A::~A()" << endl;};
  11. };
  12.  
  13. void thf() {
  14. static int n = 0;
  15. n += 1;
  16. cout << "\nthreads " << n << endl;
  17. n -= 1;
  18. return;
  19. }
  20.  
  21. int main() {
  22. const int thrn = 3;
  23. thread *func_thread = new thread[thrn];
  24.  
  25. cout << "A *a = new A[thrn]" << endl;
  26. A *a = new A[thrn];
  27. cout << "A *a = new A[thrn]" << endl;
  28.  
  29. for(auto i = 0; i < thrn; i += 1) {
  30. func_thread[i] = thread(thf);
  31.  
  32. cout << "a[i] = A(int)" << endl;
  33. a[i] = A(5);
  34. cout << "a[i] = A(int)" << endl;
  35. }
  36.  
  37. cout << "CHECK" << endl;
  38.  
  39. for(auto i = 0; i < thrn; i += 1) {
  40. auto &x = func_thread[i];
  41. if(x.joinable()) {
  42. x.join();
  43. }
  44. }
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 27992KB
stdin
Standard input is empty
stdout
A *a = new A[thrn]
A: 0x8dd3a2c A::A()
A: 0x8dd3a2d A::A()
A: 0x8dd3a2e A::A()
A *a = new A[thrn]
a[i] = A(int)
A: 0xbf95b8e8 A::A(int)
A: 0xbf95b8e8 A::~A()
a[i] = A(int)
a[i] = A(int)
A: 0xbf95b8e8 A::A(int)
A: 0xbf95b8e8 A::~A()
a[i] = A(int)
a[i] = A(int)
A: 0xbf95b8e8 A::A(int)
A: 0xbf95b8e8 A::~A()
a[i] = A(int)
CHECK

threads 1

threads 1

threads 1