memory-model

What does the [[carries_dependency]] attribute mean?

可紊 提交于 2019-11-28 16:23:07
问题 Can someone explain it in a language that mere mortals understand? 回答1: [[carries_dependency]] is used to allow dependencies to be carried across function calls. This potentially allows the compiler to generate better code when used with std::memory_order_consume for transferring values between threads on platforms with weakly-ordered architectures such as IBM's POWER architecture. In particular, if a value read with memory_order_consume is passed in to a function, then without [[carries

How are MT programs proven correct with “non sequential” semantics?

做~自己de王妃 提交于 2019-11-28 10:05:44
问题 This could be a language neutral question, but in practice I'm interested with the C++ case: how are multithread programs written in C++ versions that support MT programming, that is modern C++ with a memory model, ever proven correct? In old C++, MT programs were just written in term of pthread semantics and validated in term of the pthread rules which was conceptually easy: use primitives correctly and avoid data races. Now, the C++ language semantic is defined in term of a memory model and

Does std::mutex create a fence?

好久不见. 提交于 2019-11-28 09:38:58
If I lock a std::mutex will I always get a memory fence? I am unsure if it implies or enforces you to get the fence. Update: Found this reference following up on RMF's comments. Multithreaded programming and memory visibility Unlocking a mutex synchronizes with locking the mutex. I don't know what options the compiler has for the implementation, but you get the same effect of a fence. As I understand this is covered in: 1.10 Multi-threaded executions and data races Para 5: The library defines a number of atomic operations (Clause 29) and operations on mutexes (Clause 30) that are specially

What is the scope of memory flushed or published to various threads when using volatile and synchronized?

依然范特西╮ 提交于 2019-11-28 08:40:39
This question is in reference to memory visibility only, not happens-before and happens-after. There are four ways in Java that guarantees changes to memory in one thread to be made visible to another thread. (reference http://gee.cs.oswego.edu/dl/cpj/jmm.html ) A writing thread releases a synchronization lock and a reading thread subsequently acquires that same synchronization lock. If a field is declared as volatile, any value written to it is flushed and made visible by the writer thread before the writer thread performs any further memory operation (i.e., for the purposes at hand it is

Will two relaxed writes to the same location in different threads always be seen in the same order by other threads?

馋奶兔 提交于 2019-11-28 08:29:12
On the x86 architecture, stores to the same memory location have a total order, e.g., see this video . What are the guarantees in the C++11 memory model? More precisely, in -- Initially -- std::atomic<int> x{0}; -- Thread 1 -- x.store(1, std::memory_order_release); -- Thread 2 -- x.store(2, std::memory_order_release); -- Thread 3 -- int r1 = x.load(std::memory_order_acquire); int r2 = x.load(std::memory_order_acquire); -- Thread 4 -- int r3 = x.load(std::memory_order_acquire); int r4 = x.load(std::memory_order_acquire); would the outcome r1==1, r2==2, r3==2, r4==1 be allowed (on some

Where can I find good, solid documentation for the C++0x synchronization primitives? [closed]

半世苍凉 提交于 2019-11-28 06:54:53
I've seen articles on ::std::thread and ::std::forward and such, but I have seen no good articles on ::std::atomic . There is, of course, the standards proposal paper , but I haven't seen any good documentation for someone who just wanted to use the facility. Is there any? Where can I find it? Fred Nurk The just::thread library has decent documentation and was developed by Anthony Williams , author of C++ Concurrency in Action and maintainer of the Boost thread library. There's also the C++ Concurrency in Action book by Anthony Williams. 来源: https://stackoverflow.com/questions/4938258/where

C++11 memory_order_acquire and memory_order_release semantics?

只谈情不闲聊 提交于 2019-11-28 04:59:09
http://en.cppreference.com/w/cpp/atomic/memory_order , and other C++11 online references, define memory_order_acquire and memory_order_release as: Acquire operation: no reads in the current thread can be reordered before this load. Release operation: no writes in the current thread can be reordered after this store. This seems to allow post-acquire-writes to be executed before the acquire operation, which seems weird too me (usual acquire/release operation semantics restrict movement of all memory operations). Same online source ( http://en.cppreference.com/w/cpp/atomic/atomic_flag ) suggests

C++0x memory model and speculative loads/stores

会有一股神秘感。 提交于 2019-11-28 04:43:48
So I was reading about the memory model that is part of the upcoming C++0x standard. However, I'm a bit confused about some of the restrictions for what the compiler is allowed to do, specifically about speculative loads and stores. To start with, some of the relevant stuff: Hans Boehm's pages about threads and the memory model in C++0x Boehm, "Threads Cannot be Implemented as a Library" Boehm and Adve, "Foundations of the C++ Concurrency Memory Model" Sutter, "Prism: A Principle-Based Sequential Memory Model for Microsoft Native Code Platforms", N2197 Boehm, "Concurrency memory model compiler

C++ memory model and race conditions on char arrays

依然范特西╮ 提交于 2019-11-27 23:15:54
Basically I have trouble understanding this: (from Bjarne FAQ) However, most modern processors cannot read or write a single character, it must read or write a whole word, so the assignment to c really is ``read the word containing c, replace the c part, and write the word back again.'' Since the assignment to b is similar, there are plenty of opportunities for the two threads to clobber each other even though the threads do not (according to their source text) share data! So how can char arrays exist without 3(7?) byte padding between elements? I think Bjarne is wrong about this, or at least,

Peterson algorithm in Java?

随声附和 提交于 2019-11-27 19:26:30
Is there example implementation of Peterson algorithm for mutual exclusion in Java? No one here has provided a correct/safe implementation of this algorithm in Java. I'm not sure how John W's solution is supposed to work since it's got pieces missing (namely the declarations of the ThreadLocals and an explanation of what is supposed to be in his array—primitive booleans don't have get() and set() ). Chapter 17 of the Java Language Specification explains the Java memory model. Of particular interest is Section 17.4.5 , which describes the happens-before order. It's pretty easy to think about