'RAII: do mutexes in a vector declared in a loop all unlock in the next iteration?
Suppose I have the following:
// ... necessary includes
class X {
struct wrapper{ std::mutex mut{}; }
std::array<wrapper, 20> wrappers{};
void Y()
{
for (auto i{0u}; i < 10; ++i)
{
std::vector<std::unique_lock<std::mutex>> locks_arr{};
for (auto& wrapp : wrappers)
{
locks.emplace_back(std::unique_lock{wrapp.mut});
}
// are the mutexes in locks_arr unlocked here by RAII,
// because locks_arr goes 'out of scope'?
if (/* some condition */) continue;
// do some other long stuff
// end of loop iteration; how about here?
}
}
}
A straightforward question, elucidated in the code itself. Do the mutex locks in locks_arr unlock in the next iteration of the loop, or is there an explicit need to unlock all the mutexes one by one in both the if statement block, and at the end of the outer for-loop?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
