I have an array where each of its entries is a linked list. To avoid synchronization problems when accessing the linked lists I added a mutex to each entry.
My question is can I convert the following calls to lock
and unlock
in each iteration of the loop to one lock_guard
as shown below? Will the mutex of each entry be unlocked after each iteration? Thanks.
for(int i = 0; i < TABLE_SIZE; ++i)
{
table[i].entryMtx.lock ();
//... access the linked list of the entry...
table[i].entryMtx.unlock ();
}
// --->
for(int i = 0; i < TABLE_SIZE; ++i)
{
std::lock_guard < std::mutex > lk (table[i].entryMtx);
// ... access the linked list of the entry
}
Yes, this is how destructors are used in C++ (and other languages).
However, it's not stdx
, it's std
. Probably a typo.
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