what is correspoding feature for synchronized in java?

Don Lun picture Don Lun · Mar 25, 2011 · Viewed 23.9k times · Source

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

Thank you!

Answer

Yakov Galka picture Yakov Galka · Mar 25, 2011

Use the following in C++:

#include <mutex>

std::mutex _mutex;

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