Wake up a std::thread from usleep

后端 未结 6 1832
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 05:25

Consider the following example:

#include 
#include 
#include 

#include 
#include 

         


        
6条回答
  •  悲&欢浪女
    2021-02-14 06:28

    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);
    }
    

提交回复
热议问题