- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
~scoped_lock()
{ std::apply([](auto&... __m) { (__m.unlock(), ...); }, _M_devices); }
如何理解
[](auto&... __m) { (__m.unlock(), ...);
?我不明白
...
在 lambda 中,我不知道这是如何以相反的顺序实现释放互斥锁的。
(__m.unlock(), ...)
意味着
(__m1.unlock(),(__m2.unlock(), (__m3.unlock(), (...))))
,但不实现逆序解锁。
When control leaves the scope in which the scoped_lock object was created, the scoped_lock is destructed and the mutexes are released, in reverse order.
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
class mymutex : public std::mutex {
public:
void lock() {
std::mutex::lock();
std::cout << "mutex " << _i << " locked" << std::endl;
}
mymutex(int i): _i(i){}
bool try_lock() {
bool res = std::mutex::try_lock();
if (res) {
std::cout << "mutex " << _i << " try locked" << std::endl;
}
return res;
}
void unlock() {
std::mutex::unlock();
std::cout << "mutex " << _i << " unlocked" << std::endl;
}
private:
int _i;
};
class Speaking {
private:
int a;
mymutex my1;
mymutex my2;
mymutex my3;
public:
Speaking() : a(0), my1(1), my2(2), my3(3){};
~Speaking() = default;
void speak_without_lock();
void speak_with_three_lock();
};
void Speaking::speak_without_lock() {
std::cout << std::this_thread::get_id() << ": " << a << std::endl;
a++;
}
void Speaking::speak_with_three_lock()
{
std::scoped_lock<mymutex, mymutex, mymutex> scoped(my1, my2, my3);
speak_without_lock();
}
int main() {
Speaking s;
s.speak_with_three_lock();
return 0;
}
mutex 1 locked
mutex 2 try locked
mutex 3 try locked
1: 0
mutex 1 unlocked
mutex 2 unlocked
mutex 3 unlocked
那么 cppreference 会出错吗?
最佳答案
我相信 cppreference.com 在这个细节上是不正确的。 C++17 说:
~scoped_lock();
Effects: For all i in [0, sizeof...(MutexTypes)), get(pm).unlock()
关于c++ - 如何理解scoped_lock的析构函数?cppreference是不是出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69065454/
在 documentation of std::memory_order on cppreference.com有一个轻松排序的例子: Relaxed ordering Atomic operatio
我开始理解算法时间复杂度的表示法,例如“大 O”。但是cppreference上很多关于不同算法复杂度的描述我都看不懂,like for std::search .它不涉及我好不容易学会的符号,例如“
int main() { std::vector foo; std::atomic bar{0}; std::mutex mx; auto job = [&] {
cppreference.com std::atomic_flag列出了先前的c++ 20和c++ 20中的自旋锁的两个示例。最后修改日期是2020年7月21日,12:58。 先前的c++ 20: #
我正在审查 std::move 的变体通过在我的编译器上测试它的功能。由于某种原因,这个程序在最新的 clang++ 和 g++4.8 中都失败了。在我看来,这看起来像是一个正确的程序,应该可以工作。
我在 cppreference.com 上阅读了 std::async 的描述。第一个描述说: The template function async runs the function f asyn
以this为例description of packaged_task 来自cppreference,一个名为task的类出现。这是什么? #include #include #include
我已阅读 What does template's implicit specialization mean?和它的答案,但我仍然不满意我理解这部分Partial template specializ
我的问题是 structured binding declarations 上 cppreference.com 页面的哪一部分?应该表明它们不能与编译时未知的“事物”一起使用吗? 该页面不包含任何对
此代码是否取自 Example of Producer/Consumer with condition variable安全的?我和一位同事对于在 close() 中省略 std::unique_lo
我在 http://en.cppreference.com/w/cpp/language/partial_specialization 上找到了这个例子 template struct B {};
在 http://en.cppreference.com/w/cpp/atomic/atomic_compare_exchange , 以下示例代码作为 std::atomic_compare_exc
有人可以向我解释 cppreference 网站样本中的几点吗?该技术描述了函数重载取决于迭代器类型。前两个带有“using”的 typedef 很容易理解。与 alg 函数相关的问题: 在模板参数列
在他们的 example的 std::uninitialized_default_construct : struct S { std::string m{ "Default value" }
尝试从 cppreference.com 编译以下示例: #include struct promise; struct coroutine : std::coroutine_handle {
根据C++ Reference , mutex.lock() 是一个 memory_order_acquire 操作,而 mutex.unlock() 是一个 memory_order_release
我正在刷新我对可用的各种类型的转换的内存,并在 cppreference.com ( http://en.cppreference.com/w/cpp/language/dynamic_cast) 上
我试图编译下面的代码,但我总是得到提到的错误: #include #include typedef struct { char* Name; char* Branch; } st_
在他们的 example std::condition_variable 本质上的用法 std::mutex m; std::condition_variable cv; bool ready = f
为什么 cppreference.com 会列出 vectors' push_back() 的分摊时间复杂度但是 unordered containers' insert() 的平均和最差时间复杂度?
我是一名优秀的程序员,十分优秀!