Csinglelock有什么用
what is the use of Csinglelock?
本文关键字:什么 Csinglelock 更新时间:2023-10-16
MSDN 中提到,要锁定和解锁 CCriticalSection 对象,我们应该使用 CSingleLock。但是 CCriticalSection::lock() 和 CCriticalSection::unlock() 做同样的事情,不是吗?那么这两种方法有什么区别呢?
使用包装器将确保(几乎)无论您从锁定锁的位置做什么,当您离开函数时,它都会解锁。
考虑:
void func()
{
lock();
... plenty of lines ...
// x = 8128 happens on a Wednesday, in a month without
// r in the name, only if the day is divisible by 7 and 3.
if (x == 8128)
return;
... more lines of code ...
unlock();
}
因此,您的应用程序时不时地忘记解锁锁。
如果您遇到异常、使用 goto 等,这同样适用。
如果使用包装器,则可以保证在包装器的作用域结束时调用析构函数。这对于std::string
或std::vector
和锁一样有用。
当然,如果您使用例如longjmp
跳出上下文,则无济于事。但是你不应该使用longjmp
!
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 警告处理为错误这里有什么问题
- 什么时候调用组成单元对象的析构函数
- #定义c-预处理器常量..我做错了什么
- 努力将整数转换为链表。不知道我在这里做错了什么
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 什么时候在C++中返回常量引用是个好主意
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++避免重复声明的语法是什么
- c++库的公共头文件中应该包含什么
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- ifstream什么都没读
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 我应该使用什么来代替void作为变体中的替代类型之一
- Csinglelock有什么用