- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在阅读 Scott Meyers 所著的 Effective Modern C++ 中的条件变量,下面是文本。
std::condition_variable cv
std::mutex m
T1 (detecting task)
...
cv.notify_one();
T2 (reacting task)
...
{
std::unique_lock<std::mutex> lk(m);
cv.wait(lk);
...
}
这里作者提到如下
Mutexs are used to control access to shared data, but it's entirely possible that the detecting and reacting tasks have no need for such mediation. For example, the detecting task might be responsible for initializing a global data structure, then turning it over to reacting task for use. If the detecting task never access the data structure after initialzing it, and if the reacting task never access it before the detecting task indicates that it's ready, the two tasks will stay out of each other's way through program logic. There will be no need for mutex.
上面的文字我看不懂
作者所说的“两个任务将通过程序逻辑相互远离”是什么意思?
作者所说的不需要互斥锁是什么意思?
最佳答案
Mutex 用于解决竞争条件,例如:
A race condition occurs when two or more threads can access shared data and they try to change it at the same time
在您的情况下不会发生,因为在您的结构上完成的操作将在不同的时间范围内完成,即:不会导致任何竞争条件。由于您没有两个线程同时写入,因此您不需要互斥量。
还要考虑到大多数问题出现在您进行“先检查后执行”(例如,“检查”值是否为 X,然后“执行”以执行某项取决于该值为 X 的操作)并且另一个线程执行某项操作时到“检查”和“行为”之间的值。
关于c++ - 在 C++ scott meyers 中检测事件的条件变量用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34065308/
Meyers Singleton 是否适用于有动态库的场景? IE。一个定义单例的库,其他人使用它,每个都在自己的编译单元中? (我想这无关紧要,但具体架构是 OS X 上带有框架的应用程序) 我正在
我已经阅读了很多关于单例的文章,它们应该在什么时候使用,什么时候不应该使用,以及如何安全地实现它们。我正在用 C++11 编写,并且遇到了 Meyer 的单例延迟初始化实现,如 this questi
是否可以使用参数定义 Meyer 的单例(如 this one)? 我知道 GOF 风格的单例(如 here )是可能的, 但我似乎无法让它与 Meyer 的单例一起工作: // ... public
Meyer 的单例类蓝图在头文件 temp.h 中声明。如果 temp.h 包含在两个单独的 .cpp 文件中,那么每个文件都有自己的蓝图,并且静态的东西对其他模块(即 *.o 或 *.cpp)不可见
到以下代码: class C { public: static C& Instance() { static C c; return c; }
假设我有一个类: class C{ int x_; int y_; public: C(int x, int y): x_(x), y_(y){} }; 然后我想从一个字符串中添加结构,它
我的代码: template class Singleton { public: static T& instance() { static T obj; re
我继承了一些在 g++ 9 和 10 下编译良好的代码,但是当打开优化时,两个编译器都会出现运行时错误(也就是说,编译 -O0 有效,但编译 -Og 会给出来自 MMU 的运行时错误.) 问题是在一个
我过来了this question最近,对 Instance() 函数的实现产生了疑问: class Configuration { public: static Configuration*
上周有人指出我有一段代码是这样的: #include namespace NSTest { class SingletonClass { public: static SingletonCl
我有以下实现基本 Meyers 单调的代码: #ifndef _cConfigFile_HH #define _cConfigFile_HH class cConfigFile { public:
Singleton(Meyers 的 Singleton)的以下使用惰性初始化的实现是否线程安全? static Singleton& instance() { static Singlet
我有一个在 Meyers 单例中运行的 boost 线程。它在我的程序期间愉快地运行。 当我的 Singleton 的析构函数被调用时(当程序加载出内存时)我设置了一个标志以便线程应该退出它的循环并触
Singleton(Meyers 的 Singleton)的以下使用惰性初始化的实现是否线程安全? static Singleton& instance() { static Singlet
我正在阅读 S. Meyers 的书“Effective C++。55 specific ways...”(第 3 版)。这本书中的某些内容,在规则 11 中,我不明白。所以,在下一个代码部分: Wi
我正在关注 Item6,它是 Scott Meyers 着的 Effective STL: 50 Specific Ways to Improve Your Use of the Standard T
我只是尝试使用 Scott Meyers 在“Effectice C++ in an Embedded Environment”中建议的 placement new 运算符。 DefaultM
我正在阅读 std::shared_ptr在Effective Modern C++14 Scott Meyers 的书。这是一段代码,作者说其中可能存在资源泄漏: int computePriori
Singleton(Meyers 的 Singleton)线程的以下使用延迟初始化的实现是否安全? static Singleton& instance() { static Singlet
所以我读了很多关于为什么这个实现不是线程安全的。但是我没有找到如何使其线程安全快速的答案?使其线程安全的变体是添加互斥体(或者在某些情况下,只需关键部分就足够了),但这会使该方法慢得多。那么是否有一种
我是一名优秀的程序员,十分优秀!