我有一个数组,其中每个条目都是一个链表。为了避免访问链表时出现同步问题,我为每个条目添加了一个互斥锁。
我的问题是,我可以转换成以下调用lock,并unlock在循环一个的每次迭代lock_guard,如下图所示?每次迭代后都会解锁每个条目的互斥锁吗?谢谢。
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
}
Run Code Online (Sandbox Code Playgroud)