简体   繁体   English

抢先式多任务处理是否可以干扰C ++ 11发行获得语义?

[英]Could preemptive multitasking interfere with C++11 release-acquire semantics?

Is it possible (in theory) for a thread to execute an acquire on one CPU, then immediately get preempted and resumed on another CPU for which the acquire was never executed (and therefore never synchronized per the release-acquire semantics)? 从理论上讲,线程是否有可能在一个CPU上执行acquire ,然后立即被抢占并在从未执行过acquire另一个CPU上恢复(因此,从发行到获取的语义上就从未同步过)?

For ex. 对于前。 consider the following code which uses C++11 atomics and release-acquire memory-ordering to perform a lock-free thread-safe initialization: 考虑下面的代码,这些代码使用C ++ 11原子并release-acquire内存的顺序来执行无锁线程安全初始化:

if ( false == _initFlag.load(memory_order_acquire) ) {
    _foo = ...; // initialize global
    _bar = ...; // initialize global
    ... = ...; // initialize more globals
    _initFlag.store(true, memory_order_release);
}
// use the initialized values ...

If _initFlag.load(memory_order_acquire) returns true, then the calling thread will know that the initialized values of _foo , _bar , etc... are visible (propagated) to the CPU on which it is currently executing . 如果_initFlag.load(memory_order_acquire)返回true,则调用线程将知道_foo_bar等的初始化值对于当前正在执行的CPU是可见的(传播)。 But what if the thread is preempted immediately afterward and moved to another CPU?.. 但是,如果此后立即抢占该线程并将其移至另一个CPU,该怎么办?

Does the C++11 standard guarantee the new CPU will be synchronized? C ++ 11标准是否保证新CPU将被同步? Are there any implementations or architectures that might be vulnerable to this type of race condition? 是否有任何实现或体系结构可能会受到这种竞争条件的影响?

It is possible for it to get pre-empted and moved to another CPU after the acquire, but as far as I'm aware, the O/S has to ensure that any explicit memory ordering is preserved (this is probably something it keeps in the thread state). 它有可能被抢占并在获取后移到另一个CPU,但是据我所知,操作系统必须确保保留任何显式的内存顺序(这可能是它保留的东西)线程状态)。 Otherwise, there'd be very little chance of anything running reliably in a multi-cpu environment. 否则,几乎不可能有任何东西在多CPU环境中可靠运行。

I think the standard assumes that to be the case, on the basis it has to. 我认为该标准假设必须如此。

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

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