stdmutex

std::mutex 是一种简单的非递归同步结构,用于保护多个线程访问的数据。

    std::atomic_int temp{0};
    std::mutex _mutex;
    
    std::thread t( [&](){
                      
                      while( temp!= -1){
                          std::this_thread::sleep_for(std::chrono::seconds(5));
                          std::unique_lock<std::mutex> lock( _mutex);
                          
                              temp=0;
                      }
                  });
    
    
    while ( true )
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
        std::unique_lock<std::mutex> lock( _mutex, std::try_to_lock);
        if ( temp < INT_MAX )
            temp++;
        cout << temp << endl;
        
    }