gpt4 book ai didi

c++ - 如何解锁 boost::upgrade_to_unique_lock(由 boost::shared_mutex 制成)?

转载 作者:太空狗 更新时间:2023-10-29 20:00:18 27 4
gpt4 key购买 nike

所以我有一些 shared_mutex 并完成了这个:

        boost::upgrade_lock<boost::shared_mutex> lock(f->mutex);
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);

现在我想“解锁”或至少将其降级为:

boost::shared_lock<boost::shared_mutex> lock_r(f->mutex);

如何做这样的事情?可能吗?

最佳答案

如果您让 upgrade_to_unique_lock 超出范围,它将自动降级回升级所有权。

例如

void foo() {
boost::upgrade_lock<boost::shared_mutex> lock(f->mutex);

// Do shared operations, as mutex is held upgradeable
// ...

if(need_to_get_unique)
{
boost::upgrade_to_unique_lock<boost::shared_mutex> uniqueLock(lock);

// Do exclusive operations, as mutex is held uniquely
// ...
// At end of scope unique is released back to upgradeable
}
// Only shared operations here, as it's only held upgradeable
// ...

// At end of scope mutex is completely released
}

编辑:另一件事。如果给定的函数只需要独占锁,您可以使用 boost::unique_lock 并进行唯一锁定,而无需同时使用 upgradeupgrade_to_unique 锁.

关于c++ - 如何解锁 boost::upgrade_to_unique_lock(由 boost::shared_mutex 制成)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7704870/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com