首页 \ 问答 \ 在C11 / C ++ 11中,是否可以在同一内存上混合原子/非原子操作?(In C11/C++11, possible to mix atomic/non-atomic ops on the same memory?)

在C11 / C ++ 11中,是否可以在同一内存上混合原子/非原子操作?(In C11/C++11, possible to mix atomic/non-atomic ops on the same memory?)

是否有可能在同一个内存位置上执行原子操作和非原子操作?

我问的不是因为我真的想这样做,而是因为我想了解C11 / C ++ 11内存模型。 他们定义了如下的“数据竞赛”:

如果一个程序的执行包含一个数据争用,如果它包含两个不同线程中的冲突动作,其中至少有一个不是原子的,而且两个都不会发生在另一个线程之前。 任何这样的数据竞争都会导致未定义的行为。 - C11§5.1.2.4p25, C ++ 11§1.10 p21

它的“至少其中之一不是原子的”部分令我感到困扰。 如果不可能混合原子操作和非原子操作,它只会说“在一个不是原子的对象上”。

我看不到在原子变量上执行非原子操作的直接方式。 C ++中的std::atomic<T>没有定义任何使用非原子语义的操作。 在C中,原子变量的所有直接读取/写入似乎都被转换为原子操作。

我想memcpy()和其他直接内存操作可能是一种对原子变量执行非原子读/写的方式? 即。 memcpy(&atomicvar, othermem, sizeof(atomicvar)) ? 但是,这是甚至定义的行为? 在C ++中, std::atomic是不可复制的,所以它会被定义为memcpy()在C或C ++中的行为吗?

初始化一个原子变量(无论是通过构造函数还是atomic_init() )都被定义为不是原子的。 但是这是一次性的操作:你不允许第二次初始化一个原子变量。 放置新的或显式的析构函数调用也可能不是原子的。 但是,在所有这些情况下,它似乎并不像定义的行为那样具有可能在未初始化值上运行的并发原子操作。

对非原子变量执行原子操作看起来完全不可能:C和C ++都没有定义任何可以对非原子变量操作的原子函数。

那么这里的故事是什么? 它真的是关于memcpy() ,或初始化/销毁,或其他?


Is it possible to perform atomic and non-atomic ops on the same memory location?

I ask not because I actually want to do this, but because I'm trying to understand the C11/C++11 memory model. They define a "data race" like so:

The execution of a program contains a data race if it contains two conflicting actions in different threads, at least one of which is not atomic, and neither happens before the other. Any such data race results in undefined behavior. -- C11 §5.1.2.4 p25, C++11 § 1.10 p21

Its the "at least one of which is not atomic" part that is troubling me. If it weren't possible to mix atomic and non-atomic ops, it would just say "on an object which is not atomic."

I can't see any straightforward way of performing non-atomic operations on atomic variables. std::atomic<T> in C++ doesn't define any operations with non-atomic semantics. In C, all direct reads/writes of an atomic variable appear to be translated into atomic operations.

I suppose memcpy() and other direct memory operations might be a way of performing a non-atomic read/write on an atomic variable? ie. memcpy(&atomicvar, othermem, sizeof(atomicvar))? But is this even defined behavior? In C++, std::atomic is not copyable, so would it be defined behavior to memcpy() it in C or C++?

Initialization of an atomic variable (whether through a constructor or atomic_init()) is defined to not be atomic. But this is a one-time operation: you're not allowed to initialize an atomic variable a second time. Placement new or an explicit destructor call could would also not be atomic. But in all of these cases, it doesn't seem like it would be defined behavior anyway to have a concurrent atomic operation that might be operating on an uninitialized value.

Performing atomic operations on non-atomic variables seems totally impossible: neither C nor C++ define any atomic functions that can operate on non-atomic variables.

So what is the story here? Is it really about memcpy(), or initialization/destruction, or something else?


原文:https://stackoverflow.com/questions/35109419
更新时间:2022-03-26 13:03

最满意答案

问题是Google希望用户自己启动任何媒体,因此如果您调试设备Chrome浏览器 ,则会在HTMLMediaElement上发出警告“ 未能执行'播放':API只能由用户的手势启动。 “这意味着你需要附加视频初始化,例如,点击事件


The problem is that Google want that users initiate by themselves any media, so If you debug your device chrome browser, you will get the warning "Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated by a user gesture." So that means you need to attach the video initialization, for example, with a click event

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)