简体   繁体   English

如果仍然需要互斥锁才能正常工作,为什么要使用 std::atomic

[英]why use std::atomic if it still requires a mutex to work properly

Reading the text for std::condition_variable I've come across this sentence:阅读std::condition_variable的文本我遇到了这句话:

Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread.即使共享变量是原子的,也必须在互斥锁下进行修改,才能将修改正确发布到等待线程。

My question is this:我的问题是这样的:

What is the use for atomics if not for "lock-free code to work with PODs"?如果不是“与 POD 一起工作的无锁代码”,原子有什么用?

UPDATE更新

Looks like there's some confusion about my question :(看起来我的问题有些混乱:(

The "shared variable" in the quoted text is not the same as "condition variable".引用文本中的“共享变量”与“条件变量”不同。 See this quote from the same page:请参阅同一页面中的此引用:

... until another thread both modifies a shared variable (the condition), and notifies the condition_variable ...直到另一个线程同时修改共享变量(条件),并通知condition_variable

Please do not answer " why we need to use a mutex with condition variables " or " how the conditional wait works " but rather provide information on how the use of a mutex "correctly publishes" the modification of an atomic to the waiting thread, ie whether an expression like ++counter;请不要回答“ 为什么我们需要使用带有条件变量的互斥锁”或“ 条件等待如何工作”,而是提供有关互斥锁的使用如何“正确发布”对等待线程的原子修改的信息,即是否像++counter;这样的表达式++counter; ( not the test like if(counter == 0) ) would need to be done under mutex? 不是if(counter == 0)这样的测试)需要在互斥锁下完成吗?

The semantics of a conditional wait necessitates the use of a mutex.条件等待的语义需要使用互斥锁。

This is because there is a potential race condition on the thread that is testing the condition.这是因为在测试条件的线程上存在潜在的竞争条件。 When this thread is checking whether to wait, the following happens:当该线程检查是否等待时,会发生以下情况:

  1. acquire exclusive lock获取排他锁
  2. perform the test进行测试
  3. either:任何一个:

    a) release lock and wait for next signal a) 释放锁并等待下一个信号

    b) keep lock and continue b) 保持锁定并继续

Because step 1 acquires a lock, the whole thing is atomic, provided all other parties use the mutex correctly.因为第 1 步获取锁,所以整个事情是原子的,前提是所有其他方都正确使用互斥锁。

But what about when the variable being modified is an atomic?但是当被修改的变量是原子的时候呢?

Even if the shared variable is atomic, it must be modified under the mutex in order to correctly publish the modification to the waiting thread.即使共享变量是原子的,也必须在互斥锁下进行修改,才能将修改正确发布到等待线程。

Well, here's why.嗯,这就是原因。 If thread B comes along and modifies the atomic outside of the mutex, then steps 2 and 3 are no longer atomic.如果线程 B 出现并修改互斥锁之外的原子,则步骤 2 和 3 不再是原子的。 Essentially, thread B can modify the value immediately after step 2 happens.本质上,线程 B 可以在步骤 2 发生后立即修改该值。 Then thread A will potentially make an incorrect decision in step 3.那么线程 A 可能会在步骤 3 中做出错误的决定。

why use std::atomic if [condition variable] still requires a mutex to work properly如果 [条件变量] 仍然需要互斥体才能正常工作,为什么要使用 std::atomic

There is no reason to use an atomic condition variable.没有理由使用原子条件变量。

What is the use for atomics原子有什么用

Even if atomics aren't useful for condition variables, they can still be useful for other use cases.即使原子对条件变量没有用处,它们仍然可以用于其他用例。 A typical use case for atomics is implementation of a lock free queue.原子的典型用例是实现无锁队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM