In C#/Java I can easily make any thread-unsafe code to be thread-safe. I just introduce special "lockObj" and use it like that:
private object lockObj = new object();
void ThreadSafeMethod()
{
lock (lockObj)
{
// work with thread-unsafe code
}
}
(some people just using lock (this) but this is not recomended)
What is easiest and fastest C++ equivalent? I can use C++11.
If you can use C++11, use a std::mutex (if you can not use C++11, use boost::mutex)
private:
std::mutex m;
void ThreadSafeMethod()
{
std::lock_guard<std::mutex> lock(m);
// work with thread-unsafe code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With