I am trying to understand the monitor class that Herb Sutter presented on C++ and Beyond 2012:
template<typename T>
class monitor {
private:
mutable T t;
mutable std::mutex m;
public:
monitor(T t_ = T{}) : t{ t_ } {}
template<typename F>
auto operator()(F f) const -> decltype(f(t))
{
std::lock_guard<std::mutex> _{ m }; return f(t);
}
};
I have managed to create a class that does the same thing in a more old fashioned and simpler (for me at least) way:
template<typename T>
class MyMonitor {
public:
MyMonitor() { t = T(); }
template<typename F>
auto callFunc(F f) {
std::lock_guard<std::mutex> lock(m);
return f(t);
}
private:
T t;
std::mutex m;
};
In which ways are Herb Sutters code better than mine?