我们需要对c ++中的类的静态成员变量具有静态锁 [英] Do we need to have static lock for static member variable of the class in c++

查看:1116
本文介绍了我们需要对c ++中的类的静态成员变量具有静态锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态地图作为我类的成员变量。
当我们必须访问此地图时,我们需要有静态锁吗?

I have a static map as a member variable of my class. Do we need to have static lock when we have to access this map?

推荐答案

c> std :: map 实例被声明为类static,那么你的锁也需要是类static。

If your std::map instance is declared class static, then your lock needs to be class static too.


  • 对象1锁定本地锁并开始操作共享的对象

  • 对象2锁定其本地锁(它是一个单独的锁,记住)并开始操作共享地图。

  • Boom / crash / burn

如果锁是类static,这两个对象将共享锁,并且上述情况将正常工作,

If the lock is class static, the two objects will share the lock, and the above scenario will work well, only one thread can lock at a time.

当然还有其他方法可以共享锁,而不使用 static 似乎不是你要的。

There are of course other ways to share a lock without using static, but that does not seem to be what you're asking.

这篇关于我们需要对c ++中的类的静态成员变量具有静态锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆