- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在想。当我使用一个std::mutex _lock
并想依赖守卫来释放它时,我可以这样使用吗?
class MyClass{
private:
mutable std::mutex _lock;
void subroutine(){/*...*/}
public:
void foo(){
std::lock(_lock);std::lock_guard<std::mutex> g(_lock, std::adopt_lock);
subroutine();
//require I still have the lock here
//...
return; //g goes out of scope ==> _lock is released
}
};
或者对 subroutine
的调用是否已经导致锁被释放?如果事实证明是后者,我有什么选择?
更新
这个案例呢?
class MyClass{
private:
mutable std::mutex _lock;
public:
void subroutine(){ //can be called on its own
std::lock(_lock);std::lock_guard<std::mutex> g(_lock, std::adopt_lock);
/*...*/
}
void foo(){
std::lock(_lock);std::lock_guard<std::mutex> g(_lock, std::adopt_lock);
subroutine();
//require I still have the lock here
//...
return; //g goes out of scope ==> _lock is released
}
};
最佳答案
调用子程序不会导致锁被释放。当 std::lock_guard 对象超出范围时(正如您在评论中提到的那样),线程对 _lock 的锁定被释放。
void foo(){
std::lock_guard<std::mutex> lg{_lock}; // lock the mutex
subroutine(); // lock is held here during this call
// so subroutine must not attempt to lock the mutex _lock!
return; // lg goes out of scope => its destructor is called which releases the lock on _lock
}
要回答您的第二个问题,当 foo 获取 _lock 上的锁然后调用子例程,子例程再次尝试锁定 _lock 时,您遇到了问题。线程无法在它已经持有锁的互斥锁上获取锁。我会重新考虑你的类(class)设计。如果你绝对必须有这种设计,你可以做类似的事情
void subroutine(bool called_from_foo = false)
{
if(!called_from_foo)
std::lock_guard<std::mutex> lg{_lock};
/* ... */
}
void foo()
{
std::lock_guard<std::mutex> lg{_lock};
subroutine(true);
/* ... */
return;
}
尽管我不推荐这样做,因为它很容易以错误的方式使用子例程,导致程序出现未定义的行为。
如果您发现自己必须在函数之间传递锁的所有者,我会看一下 std::unique_lock,因为这种类型是可移动的。
关于c++:互斥、守卫和函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33722020/
我如何使用 if 守卫的理解? type Error = String type Success = String def csrfValidation(session:Session,
我试图深入理解 Angular,所以我阅读了 the docs这非常有帮助。 现在我正在研究守卫。我在文档中阅读了此声明。 The router checks the CanDeactivate an
是否可以批量guard's 观看通知? 例如,如果移动了一个子文件夹,watch 会为每个文件发出一个事件。我真正想要的是一个通知,而不是几个,如果有什么变化。 最佳答案 虽然这不是批量更改,因此没有
我正在使用 Guard gem 在开发的某些时候,我只需要跟踪一个特定的文件或几个文件,而不是整个项目。 是否有一些方便的方法来临时跟踪特定文件? 我知道这可以通过修改保护文件来完成,但我认为这不是一
已解决 真正帮助我的是我可以#include .cpp 文件中的 header 而不会导致重新定义的错误。 我是 C++ 新手,但我有一些 C# 和 Java 编程经验,所以我可能会遗漏一些 C++
是否有可能保证在范围退出时完成的变量。 具体来说,我想要一个守卫:在初始化时调用特定函数,并在作用域退出时调用另一个特定函数的东西。 最佳答案 这最好明确地完成: class Guard def
我有一个类型代表我的应用程序的游戏状态,对于这个问题假装它很简单,例如: Game { points :: Int } 我用 State monad 定义我的游戏逻辑。 type GameState
这个问题在这里已经有了答案: Why is GHC complaining about non-exhaustive patterns? (3 个答案) 关闭 7 个月前。 我有以下代码,它定义了一
我知道在头文件中使用 include guards 是为了防止某些东西被定义两次。不过,使用此代码示例完全没问题: foo.c #include #include #include "bar.h"
这个问题在这里已经有了答案: Why is GHC complaining about non-exhaustive patterns? (3 个答案) 关闭 7 个月前。 我有以下代码,它定义了一
我知道 guard 在 Swift 中的作用。我已经阅读了有关使用 guard 或 if let 的问题。但是,guard (condition) else { return } 和if !condi
我正在试验 Xcode 提供的不同分析选项,但是当我在 Diagnostics 选项卡中启用 Guard Malloc 选项并尝试运行时,我收到了这个错误立即崩溃: dyld: could not l
这是我的 canDeactivate 守卫,它可以工作。但是我不想在使用提交按钮时调用守卫。只有当我通过任何其他方式导航时。怎么办? import { Injectable } from '@angu
嗨,这让我发疯。找了半天也没找到解决方法。 如何为 Guardfile 中的所有守卫触发“run_all”。 当我在 shell 中运行“guard”时,我希望它假装所有文件都已更改并触发所有守卫。
我想知道是否有守卫(断言)函数的 golang 命名约定?我用谷歌搜索了一下,但找不到任何确定的东西。我在“The Go Programming Language”一书中读到,使用“必须”前缀是一种常
我正在尝试使用 guard 的 --listen-on带有 vagrant 的选项,如概述 here ,但我无法让它工作。 如果我添加 config.vm.network :forwarded_por
我目前有一个路线守卫,例如 export class EntityGuard implements CanActivate { constructor(private readonly route
我正尝试在 less 中创建一个高度可定制的按钮 mixin。例如,我希望客户能够进入他们的 .less 文件并写入: .my-button { .btn(@bg: #FFF, @font:
这个问题在这里已经有了答案: #pragma once vs include guards? [duplicate] (13 个答案) 关闭 3 年前。 我正在浏览 Implementation d
这个问题在这里已经有了答案: Is #pragma once a safe include guard? (15 个答案) 关闭 3 年前。 我正在开发一个已知只能在 Windows 上运行并在 V
我是一名优秀的程序员,十分优秀!