Wake up a std::thread from usleep

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

Consider the following example:

#include 
#include 
#include 

#include 
#include 

         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-14 06:12

    One possible approach:(There are many ways to accomplish..also its not good idea to use sleep in your thread)

    ///Define a mutex
    void sleepy()
    {
        //try to take mutex lock which this thread will get if main thread leaves that
        //usleep(1.0E15);
    }
    
    
    int main()
    {
        //Init the Mutex
        //take mutex lock
        std :: thread sleepy_thread(sleepy);
    
        //Do your work
        //unlock the mutex...This will enable the sleepy thread to run
        sleepy_thread.join();
    }
    

提交回复
热议问题