简体   繁体   English

是std :: atomic <T> 当std :: atomic时中断安全 <T> :: is_always_lock_free是假的?

[英]Is std::atomic<T> safe with interrupts when std::atomic<T>::is_always_lock_free is false?

In an embedded (ARM) environment with no OS, if I use interrupts, then is there potential for deadlock using std::atomic<T> ? 在没有操作系统的嵌入式(ARM)环境中,如果我使用中断,那么使用std::atomic<T>是否有可能导致死锁? If so, how? 如果是这样,怎么样?

In general, any moment, control can be interrupted to handle an interrupt. 通常,任何时刻,控制都可以被中断以处理中断。 In particular, if one were to naively have a mutex and wanted to use it to do a "safe" to a variable, one might lock it, write, and unlock and then elsewhere lock, read, and unlock. 特别是,如果一个人天真地拥有一个互斥体并希望用它来对变量做一个“安全”,那么可以锁定它,写入和解锁,然后锁定,读取和解锁。 But if the read is in an interrupt, you could lock, interrupt, lock => deadlock. 但是如果读取是在一个中断中,你可以锁定,中断,锁定=>死锁。

In particular, I have a std::atomic<int> for which is_always_lock_free is false . 特别是,我有一个std::atomic<int> ,其中is_always_lock_freefalse Should I worry about the deadlock case? 我应该担心僵局吗? When I look at the generated assembly, writing 42 looks like: 当我查看生成的程序集时,编写42看起来像:

bl __sync_synchronize
mov r3, #42
str r3, [sp, #4]
bl __sync_synchronize

which doesn't appear to be locking. 它似乎没有锁定。 The asm for reading the value is similar. 用于读取值的asm是类似的。 Is the (possible) lock for the fancier operations like exchange ? 对于像exchange这样的发烧友操作(可能)锁定?

__sync_synchronize is just a builtin for a full memory barrier. __sync_synchronize只是一个完整内存屏障的内置 __sync_synchronize There is no locking involved, so no potential for deadlock as there would be with a mutex and interrupt handler. 没有涉及锁定,因此没有潜在的死锁,因为会有互斥和中断处理程序。

What ARM core are you using? 您使用的是什么ARM内核? On an ARM Cortex-A7 the following prints true for both. 上了ARM Cortex-A7下列打印true两个。

#include <iostream>
#include <atomic>

int main()
{
   std::atomic<int> x;
   std::cout << std::boolalpha << x.is_lock_free() << std::endl;
   std::cout << std::atomic<int>::is_always_lock_free << std::endl;
}

I would expect std::atomic<int> to be implemented without locks most if not all on ARM, and certainly from the assembly you provided it does not appear to be using a lock. 我希望std::atomic<int>在没有锁的情况下实现,如果不是全部在ARM上,并且当然从你提供的程序集看起来似乎没有使用锁。

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

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