- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设我有这样的代码:
#include "boost/thread/mutex.hpp"
using boost::mutex;
typedef mutex::scoped_lock lock;
mutex mut1, mut2;
void Func() {
// ...
}
void test_raiicomma_1() {
lock mut1_lock(mut1);
Func();
}
void test_raiicomma_2() {
(lock(mut1)), Func();
}
void test_raiicomma_3() {
(lock(mut1)), (lock(mut2)), Func(); // Warning!
}
int main()
{
test_raiicomma_1();
test_raiicomma_2();
test_raiicomma_3();
return 0;
}
如果函数 test_raiicomma_1
() 被多个线程调用,它会锁定一个互斥量以防止任何其他线程同时调用 Func()
。互斥锁在构造变量 mut1_lock
时被锁定,并在超出范围并被析构时释放。
这工作得很好,但就风格而言,需要为持有锁的临时对象命名让我很烦。函数 test_raiicomma_2()
试图通过初始化锁对象并在一个表达式中调用函数 Func()
来避免这种情况。
在 Func()
返回后,直到表达式结束才会调用临时对象析构函数是否正确? (如果是这样,您认为使用这个习惯用法是否值得,或者在单独的语句中声明锁是否总是更清楚?)
如果函数 test_raiicomma_3()
需要锁定两个互斥量,互斥量在调用 Func()
之前按顺序锁定,然后释放,是否正确,但不幸的是,可能会以任一顺序发布?
最佳答案
Is it correct that the temporary object destructor will not be called until the end of the expression, after Func() has returned?
保证构造函数和析构函数都被调用,因为它们有副作用,并且破坏只会发生在完整表达式的末尾。
我相信它应该有效
If the function test_raiicomma_3() needs to lock two mutexes, is it correct that the mutexes will be locked in order before calling Func(), and released afterwards, but may unfortunately be released in either order?
逗号总是从左到右求值,范围内的自动变量总是以创建的相反顺序销毁,所以我认为甚至可以保证它们也以(正确的)顺序释放
正如 litb 在评论中指出的那样,您需要大括号,否则您的表达式将被解析为声明。
(If so, do you think it's ever worthwhile to use this idiom, or is it always clearer to declare the lock in a separate statement?)
我不这么认为,为了获得非常非常少的 yield 而混淆......我总是使用非常明确的锁和非常明确的范围(通常在一个 block 中使用额外的 {}),体面的线程安全代码在没有“特殊”代码的情况下已经足够困难,并且在我看来保证代码非常清晰。
当然是 YMMW :)
关于c++ - 在逗号运算符的 LHS 中初始化匿名互斥锁持有类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1393454/
我知道 a = val1 if condition else val2 但是有没有办法做类似的事情 a if condition else b = val 抛出一个SyntaxError(我想这是可以
我遇到了一些归结为以下内容的代码: enum BAR { /* enum values omitted */ } class Foo{ public: void set(const BAR& ba
我是 R 新手,我想用 *apply 改进以下脚本函数(我已经阅读了 apply ,但我无法使用它)。我想用lm在多个自变量(数据框中的列)上运行。我用了 for (i in (1:3) { as
我知道要问这种有趣的问题。我想知道这是否可以做到? Class foo { public static void main(String [] args){ for (int i=0; i = new
分配给表达式(而不是名称)在 Python 中很常见。例如,这是完全有效的语法: my.object["with_some"].very_long["expression"] = func(my.ob
我想在 R 中的一行中分配多个变量。可以这样做吗? values # initialize some vector of values (a, b) = values[c(2,4)] # assign
以下程序的输出未给出预期结果: #include int main() { int *x; int *y; *x = 10; *y = 45; printf
我正在学习流口水并试图了解最佳实践。之间有什么实际区别: when $event : Event(Helper.notNull(foo),...) 和 when $event : Ev
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 年前。 Improve
给出以下类: template class table2D { ... public: bool copy(int r, int c, int rows , int cols, ta
我知道以下关于引用 例如。 int &ref=x;然后 ref 和 x 是内存中相同位置的名称。与指针不同,内存不是为引用分配的。 我正在使用我成功编写的引用在 C++ 中编写一个简单的交换程序。 然
尝试在 Python + Gurobi 中实现指标约束,其中指标(LHS)是二元决策变量的总和。 嗨,我想在 Python + Gurobi 中实现以下功能: Y_i_d and U_d are bi
假设我有这样的代码: #include "boost/thread/mutex.hpp" using boost::mutex; typedef mutex::scoped_lock lock; mu
这个问题在这里已经有了答案: In f(x), can x be evaluated before f? (2 个答案) 关闭 5 年前。 我已阅读 Order of evalution来自cppr
我正在学习 C++ 异常,我想对场景进行一些说明: T function() throw(std::exception); ... T t = value; try { t = function();
有谁知道 Mathematica 中是否有一个内置函数来获取降值规则的 lhs(没有任何持有)?我知道如何编写代码来做到这一点,但对于内置的 例如: a[1]=2; a[2]=3; BuiltInID
我碰巧真的很喜欢 Markdown(可能是因为 SO)而且我喜欢用 Haskell 编程。我最近发现了Literate Haskell (LHS) 我想同时使用 Markdown 和 LHS。让我给你
我想从 arules 生成的规则中提取 lhs 项。 例如, {a,b,c} => {d} 我希望能够提取 a,b,c 并将其放入字符向量中,以便我可以根据这些项目进行迭代和进一步处理。 目前,我可以
sap.ui.core.Control.extend("control.linechart", { /* the control API */ metadata : {
我正在尝试编写一个使用迭代深化算法来解决规划问题的 CLIPS 程序。出于同样的原因,我想保持较低的分支因子。 在以下代码中?s是表示树的级别的变量;我想使用一个规则来进行不同的检查。这就是我试图做的
我是一名优秀的程序员,十分优秀!