memory-model

What is the C++03 memory model for concurrency?

﹥>﹥吖頭↗ 提交于 2019-11-29 20:37:57
What is the memory model for concurrency in C++03? (And, does C++11 change the memory model to support concurrency better?) bltxd The C++ memory model is the specification of when and why physical memory is read/written with respect to C++ code. Until the next C++ standard, the C++ memory model is the same as C. In the C++0x standard, a proper memory model for multithreading is expected to be included (see here ), and it will be part possibly of the next revision of the C standard, C1X. The current one is rudimentary: it only specifies the behavior of memory operations observable by the

What does the [[carries_dependency]] attribute mean?

。_饼干妹妹 提交于 2019-11-29 20:22:56
Can someone explain it in a language that mere mortals understand? Anthony Williams [[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_dependency]] , then the compiler may have to issue a memory fence instruction to guarantee that

What does “release sequence” mean?

半城伤御伤魂 提交于 2019-11-29 09:16:17
问题 I don't understand, why will be problems without release sequence , if we have 2 threads in the example below. We have only 2 operations on the atomic variable count . count is decremented sequently as shown in the output. From C++ Concurrency in Action by Antony Williams : I mentioned that you could get a synchronizes-with relationship between a store to an atomic variable and a load of that atomic variable from another thread, even when there’s a sequence of read-modify-write operations

Does Delphi have any equivalent to C's volatile variable?

≯℡__Kan透↙ 提交于 2019-11-29 03:42:47
In C and C++ a variable can be marked as volatile , which means the compiler will not optimize it because it may be modified external to the declaring object. Is there an equivalent in Delphi programming? If not a keyword, maybe a work around? My thought was to use Absolute , but I wasn't sure, and that may introduce other side effects. Short answer: no. However, I am not aware of any situation in which the conservative approach of the compiler will change the number of reads or writes if you follow this approach: When reading a cross-thread visible location, save its value to a local before

In C/C++, are volatile variables guaranteed to have eventually consistent semantics betwen threads?

本小妞迷上赌 提交于 2019-11-29 02:01:26
Is there any guarantee by any commonly followed standard (ISO C or C++, or any of the POSIX/SUS specifications) that a variable (perhaps marked volatile), not guarded by a mutex, that is being accessed by multiple threads will become eventually consistent if it is assigned to? To provide a specific example, consider two threads sharing a variable v, with initial value zero. Thread 1: v = 1 Thread 2: while(v == 0) yield(); Is thread 2 guaranteed to terminate eventually? Or can it conceivably spin forever because the cache coherency never kicks in and makes the assignment visible in thread 2's

Release/Acquire semantics wrt std::mutex

谁都会走 提交于 2019-11-28 23:45:56
I am reading the C++ memory model defined in n3485 and it talks about release/acquire semantics, which from what I understand, and also from the definitions given in this blog : Acquire semantics is a property which can only apply to operations which read from shared memory, whether they are read-modify-write operations or plain loads. The operation is then considered a read-acquire. Acquire semantics prevent memory reordering of the read-acquire with any read or write operation which follows it in program order. Release semantics is a property which can only apply to operations which write to

How do memory_order_seq_cst and memory_order_acq_rel differ?

末鹿安然 提交于 2019-11-28 20:58:45
问题 Stores are release operations and loads are acquire operations for both. I know that memory_order_seq_cst is meant to impose an additional total ordering for all operations, but I'm failing to build an example where it isn't the case if all the memory_order_seq_cst are replaced by memory_order_acq_rel . Do I miss something, or the difference is just a documentation effect, i.e. one should use memory_order_seq_cst if one intend not to play with a more relaxed model and use memory_order_acq_rel

Is synchronizing with `std::mutex` slower than with `std::atomic(memory_order_seq_cst)`?

无人久伴 提交于 2019-11-28 17:02:35
The main reason for using atomics over mutexes, is that mutexes are expensive but with the default memory model for atomics being memory_order_seq_cst , isn't this just as expensive? Question: Can concurrent a program using locks be as fast as concurrent lock-free program? If so, it may not be worth the effort unless I want to use memory_order_acq_rel for atomics. Edit: I may be missing something but lock-based cant be faster than lock-free because each lock will have to be a full memory barrier too. But with lock-free, it's possible to use techniques that are less restrictive then memory

What is the C++03 memory model for concurrency?

风格不统一 提交于 2019-11-28 16:42:49
问题 What is the memory model for concurrency in C++03? (And, does C++11 change the memory model to support concurrency better?) 回答1: The C++ memory model is the specification of when and why physical memory is read/written with respect to C++ code. Until the next C++ standard, the C++ memory model is the same as C. In the C++0x standard, a proper memory model for multithreading is expected to be included (see here), and it will be part possibly of the next revision of the C standard, C1X. The

What does `std::kill_dependency` do, and why would I want to use it?

故事扮演 提交于 2019-11-28 16:38:15
I've been reading about the new C++11 memory model and I've come upon the std::kill_dependency function (§29.3/14-15). I'm struggling to understand why I would ever want to use it. I found an example in the N2664 proposal but it didn't help much. It starts by showing code without std::kill_dependency . Here, the first line carries a dependency into the second, which carries a dependency into the indexing operation, and then carries a dependency into the do_something_with function. r1 = x.load(memory_order_consume); r2 = r1->index; do_something_with(a[r2]); There is further example that uses