Can the thead joinable-join have a race condition? how do you get around it?

StackOverflow https://stackoverflow.com/questions/22843655

  •  27-06-2023
  •  | 
  •  

문제

Lets say I have the following class

 class A
 {
 public:
    A()
    {
       my_thread=std::thread(std::bind(&A::foo, this));
    }
    ~A()
    {
       if (my_thread.joinable())
       {
          my_thread.join();
       }
    }
 private:
    std::thread my_thread;
    int foo();
 };

Basically, if my thread completes between the joinable and join calls, then my_thread.join will wait forever? How do you get around this?

도움이 되었습니까?

해결책

Basically, if my thread completes between the joinable and join calls, then my_thread.join will wait forever?

No. A thread is still joinable after it has completed; it only becomes unjoinable once it has been joined or detached.

All threads must be joined or detached before the controlling thread object is destroyed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top