I have a question regarding the term thread-safety. Let me give an example:
#include
#include
/// A thread-safe vector
class Th
IMHO:
This is both safe and useful:
void add(double d) {
std::lock_guard lg(m);
v.emplace_back(d);
}
This is safe but useless:
// return length of vector
int length() {
std::lock_guard lg(m);
return v.size();
}
Because by the time you've got your length it may well have changed, so reasoning about it is unlikely to be useful.
How about this?
template
decltype(auto) do_safely(Func&& f)
{
std::lock_guard lock(m);
return f(v);
}
called like this:
myv.do_safely([](auto& vec) {
// do something with the vector
return true; // or anything you like
});