Wake up a std::thread from usleep

后端 未结 6 2402
借酒劲吻你
借酒劲吻你 2021-02-14 05:28

Consider the following example:

#include 
#include 
#include 

#include 
#include 

         


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-14 06:19

    Sleep for a short amount of time and look to see if a variable has changed.

    #include 
    #include 
    #include 
    
    std::atomic sharedVar(1);
    void sleepy()
    {
        while (sharedVar.load())
        {
            usleep(500);
        }
    }
    
    int main()
    {
        std :: thread sleepy_thread(sleepy);
        // wake up
        sharedVar.store(0);
    }
    

提交回复
热议问题