what is correspoding feature for synchronized in java?

后端 未结 4 717
谎友^
谎友^ 2020-12-08 04:12

synchronized in Java can guarantee safety of thread. What about C++?

Thank you!

4条回答
  •  孤街浪徒
    2020-12-08 04:47

    Use the following in C++:

    #include 
    
    std::mutex _mutex;
    
    void f()
    {
         std::unique_lock lock(_mutex);
         // access your resource here.
    }
    

提交回复
热议问题