[英]Does Making std::mutex as static creates race-condition for the mutex itself
It may sound dummy but,Am sort of confused, I have gone through this question ,when looking into it we both where in the same situation it seems, I have to make my map
as static so it will be common to all instances that will be created in separate threads
and I want to synchronize the functions that gonna act on my map, so i thought of making a std::mutex
as static
in my class like what was suggested as an answer in the given link.. in this case will there be any race-condition occur for acquiring and locking the mutex
itself? 它可能听起来很虚,但有点困惑,我已经经历过这个问题 ,当我们看到它们时,我们两个在相同的情况下,我必须使我的
map
为静态所以它将是所有实例将是共同的在单独的threads
创建,我想同步将在我的地图上执行的函数,所以我想在我的类中将std::mutex
为static
,就像在给定链接中建议作为答案一样..在这种情况下获取和锁定mutex
本身是否会出现任何竞争条件? is there any better way we can synchronize the functions on static map
using mutex
有没有更好的方法我们可以使用
mutex
同步static map
上的功能
Does Making
std::mutex
as static creates race-condition for the mutex itself将
std::mutex
设为静态会为互斥锁本身创建竞争条件
No, a Mutex isn't vulnerable to race-conditions. 不, Mutex不易受竞争条件的影响。 And as for initializing it as
static
, you are safe. 至于将其初始化为
static
,您就是安全的。
$6.7: 4 : Dynamic initialization of a block-scope variable with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) is performed the first time control passes through its declaration;
$ 6.7:4 :第一次控制通过其声明时,执行具有静态存储持续时间([basic.stc.static])或线程存储持续时间([basic.stc.thread])的块范围变量的动态初始化。 such a variable is considered initialized upon the completion of its initialization.
这样的变量在初始化完成后被认为是初始化的。 If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration.
如果通过抛出异常退出初始化,则初始化未完成,因此下次控制进入声明时将再次尝试初始化。 If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization
如果控制在初始化变量时同时进入声明,则并发执行应等待初始化完成
You said: 你说:
i thought of making a
std::mutex
as static in my class like what was suggested as an answer in the given link.我想在我的班级中将
std::mutex
设为静态,就像在给定链接中建议的那样。
Do that if you are trying to protect static
class member variables as well. 如果您尝试保护
static
类成员变量,请执行此操作。 Otherwise, make it a mutable
member. 否则,将其
mutable
成员。 The fact that you said the map
will be globally initialized as static
is okay, since the mutex as a member variable, will follow suite. 您说
map
将全局初始化为static
的事实是可以的,因为互斥体作为成员变量,将遵循套件。
class Map{
public:
Map(...){}
std::size_t size() const{
std::lock_guard<std::mutex> lck(m_m);
return m_size;
}
iterator add(....) {
std::lock_guard<std::mutex> lck(m_m);
....
return your_iterator;
}
...etc
private:
mutable std::mutex m_m; //FREE ADVICE: Use a std::recursive_mutex instead
...others
};
Now: 现在:
//Somewhere at global scope:
Map mp(... ...);
// NOTES
// 1. `mp` will be initialized in a thread safe way by the runtime.
// 2. Since you've protected all Read or Write member functions of the class `Map`,
// you are safe to call it from any function and from any thread
No. 没有。
Mutexes (and other synchronisation primitives) are implemented using support from the operating system. 互斥体(和其他同步原语)是使用操作系统的支持实现的。 That's the only way that they can do their job.
这是他们完成工作的唯一途径。
A direct corollorary of their ability to perform this job is that they are themselves not prone to race conditions — locking and unlocking operations on mutexes are atomic. 他们执行这项工作的能力的直接证据是他们自己不容易出现竞争条件 - 对互斥锁的锁定和解锁操作是原子的。
Otherwise, they wouldn't be much use! 否则,他们就没用多少了! Every time you used a mutex, you'd have to protect it with another mutex, then protect that mutex with another mutex, and so on and so forth until you had an infinite number of mutexes, none of them actually achieving anything of any use at all.
每次使用互斥锁时,都必须使用另一个互斥锁保护它,然后用另一个互斥锁保护该互斥锁,依此类推,直到你拥有无限数量的互斥锁为止,它们实际上都没有实现任何用途一点都不 :)
:)
The std::mutex
object having static storage duration doesn't change this in any way. 具有静态存储持续时间的
std::mutex
对象不会以任何方式更改此设置。 Presumably you were thinking of function- static
variables (that, assuming they're not already immune to race conditions, must be synchronised because they may be accessed concurrently by different threads; but still, ideally you wouldn't use them at all because they make functions not be re-entrant). 大概是你在考虑函数
static
变量(假设它们已经不能满足竞争条件,必须同步,因为它们可能被不同的线程同时访问;但是理想情况下你根本不会使用它们,因为它们使函数不可重入)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.