问题标签 [scoped-lock]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
715 浏览

c++ - boost 无法使用 gcc 移动 scoped_lock

以下在 VS2010 (Express) 下编译,但不是 gcc(此处为 4.6.2)。

可锁定的.h:

主.cpp:

错误:

据我了解,在LockedProxy's move-constructor 中, thescoped_lock不是移动而是复制构造的,这确实不应该工作。不应该std::move保证它的移动建设吗?我错过了什么?

0 投票
1 回答
61 浏览

c++ - creating scoped_lock for 200 ms max

I am trying to create a timed scoped lock on mutex. I thought following api from boost could help but I am having hard time finding some sample code as reference to use it.

I want to do it something like this,

My question is how to create a posix_time::ptime object of duration 200ms. I tried to create time_duration object but their is no constructor that accepts it straight forward way.

Can someone help me understand how it works please?

0 投票
2 回答
54636 浏览

c++ - 增强互斥锁

我正在阅读 drdobbs.com 上的 Boost Mutex 教程,并找到了这段代码:

现在我明白了 Mutex 的意义在于防止两个线程同时访问同一个资源,但我没有看到 io_mutex 和 std::cout 之间的相关性。这段代码是否只是锁定范围内的所有内容,直到范围完成?

0 投票
1 回答
4391 浏览

c++ - C++ 增强线程和互斥锁

我刚开始升压。我想问一下我的代码是否很好地使用了互斥锁。为了测试它,我编写了计算数字 1 到 n 的总和的代码。计算它的方法很愚蠢,但我使用了 n 个线程......只是为了尝试互斥锁......

结果看起来不错,没有互斥锁看起来很糟糕,所以我认为我正确使用了它,但只是想问一下是否一切正常。感谢您的任何建议;我希望它对我和其他人有帮助。

0 投票
0 回答
57 浏览

c++ - std::lock_guard 什么时候会被锁定?

我有以下代码。我想知道作用域锁是否在函数“do_something”的开头被锁定?非常感谢!

0 投票
4 回答
2670 浏览

go - golang 中是否有惯用的范围语义?

我想知道是否有任何惯用的方式来表示范围语义。我所说的范围是指:

  • 作用域互斥锁(oneliner 而不是显式 Lock + defered Unlock),
  • 记录功能(或任何代码块)的入口和出口,
  • 测量执行时间。

前两个项目符号的示例代码:

https://play.golang.org/p/33j-GrBWSq

基本上我们现在需要进行一个函数调用(例如互斥锁),而一个调用应该推迟到延迟(例如互斥解锁)。我建议在这里只返回未命名的函数,但它可以很容易地命名(返回带有函数字段的结构)。

只有一个问题:用户可能会忘记“调用”第一次调用的结果。

这段代码(可以)是惯用的吗?

0 投票
1 回答
207 浏览

c++ - boost::scoped_lock 不起作用(对我来说)

我正在扩展一个代码库,请查看以下从类中提取的代码片段。为了不让您感到困惑,我尽可能简单:

我在标准输出中得到了这个:

....等等。signal() 不叫。我打印this以表明我们正在对同一个对象进行操作。

有多少可能/在什么样的情况下会发生?`'foo 最初被输入两次(并且与其余逻辑混淆),同时它受到互斥锁的保护!

我很欣赏你的解决方案。谢谢

0 投票
3 回答
295 浏览

c++ - boost::scoped_lock RAII behaviour

From a Container class, I'd like to lock a vector of boost::mutex, each one owned by a Controlled instance (weird code design, but for MWE purpose only).

But since processing is cpu-bound and Controlled objects are modified from elsewhere in the mean time, I'd like to simply do a cycled scoped_lock at the beginning of the main_method, in order to lock everything and asap, such as

Problem is, if I understanded well the RAII idiom and the scoped_lock context, that in this way, the locks would go out of scope soon after the lock for cycle ends.

I've tried to new an array of locks at Container ctor and to delete it at its dtor, but I guess this is against the RAII idiom itself.

What did I misunderstand, or how could I refactor the whole issue?

0 投票
2 回答
387 浏览

c++ - 无法解释的行为 boost::scoped_lock

我写在 C++ 多线程 TCP 服务器上,用于使用 boost:scoped_lock 进行同步

连接到服务器客户端后冻结。

在 gdb 中,我在调用 boost::mutex::lock 后在 pthread_kill 中看到了更多线程

行 device.cpp:639 处的代码

pool_device 中的代码

为什么调用互斥锁线程终止后?我认为死锁不是这种行为的原因请帮助!

0 投票
0 回答
743 浏览

c++ - 对 unique_lock 和 scoped_lock 使用相同的互斥锁

使用具有相同互斥锁的 unique_lock 和 scoped_lock 是否合适?允许使用 cv.wait 和可选解锁,同时还提供范围绑定的安全性。

例如;