There is a class template Foo<T>
. And for some specific type, a function should use lock_guard
.
Here is the example code:
#include <type_traits>
#include <mutex>
#include <vector>
template<typename T>
class Foo {
public:
void do_something(int k) {
if constexpr(std::is_same_v<T, NeedMutexType>) {
std::lock_guard<std::mutex> lock(mtx_);
}
resource_.push_back(k);
// code for task with resource_ ...
}
private:
std::mutex mtx_;
std::vector<int> resource_;
};
The std::lock_guard
will destructed in the end of if constexpr scope. (If it's not true, please correct me.)
To handle this, I can copy the code for task with resource_
below into the if constexpr scope, or just use the raw std::mutex
such as mtx_.lock()
& mtx_.unlock()
instead.
Is there any some ways to handle this with std::lock_guard
? Thanks.