memory-model

Does C have an equivalent of std::less from C++?

不想你离开。 提交于 2020-01-01 23:13:33
问题 I was recently answering a question on the undefined behaviour of doing p < q in C when p and q are pointers into different objects/arrays. That got me thinking: C++ has the same (undefined) behaviour of < in this case, but also offers the standard library template std::less which is guaranteed to return the same thing as < when the pointers can be compared, and return some consistent ordering when they cannot. Does C offer something with similar functionality which would allow safely

How do data caches route the object in this example?

限于喜欢 提交于 2019-12-31 00:58:10
问题 Consider the diagrammed data cache architecture. (ASCII art follows.) -------------------------------------- | CPU core A | CPU core B | | |------------|------------| Devices | | Cache A1 | Cache B1 | with DMA | |-------------------------| | | Cache 2 | | |------------------------------------| | RAM | -------------------------------------- Suppose that an object is shadowed on a dirty line of Cache A1, an older version of the same object is shadowed on a clean line of Cache 2, and the newest

C++20 std::atomic<float>- std::atomic<double>.specializations

早过忘川 提交于 2019-12-29 09:33:07
问题 C++20 includes specializations for atomic<float> and atomic<double> . Can anyone here explain for what practical purpose this should be good for? The only purpose I can imagine is when I have a thread that changes an atomic double or float asynchronously at random points and other threads read this values asynchronously (but a volatile double or float should in fact do the same on most platforms). But the need for this should be extremely rare. I think this rare case couldn't justify an

Is writing a reference atomic on 64bit VMs

寵の児 提交于 2019-12-28 05:18:09
问题 The java memory model mandates that writing a int is atomic: That is, if you write a value to it (consisting of 4 bytes) in one thread and read it in another, you will get all bytes or none, but never 2 new bytes and 2 old bytes or such. This is not guaranteed for long . Here, writing 0x1122334455667788 to a variable holding 0 before could result in another thread reading 0x112233440000000 or 0x0000000055667788 . Now the specification does not mandate object references to be either int or

Is writing a reference atomic on 64bit VMs

若如初见. 提交于 2019-12-28 05:17:04
问题 The java memory model mandates that writing a int is atomic: That is, if you write a value to it (consisting of 4 bytes) in one thread and read it in another, you will get all bytes or none, but never 2 new bytes and 2 old bytes or such. This is not guaranteed for long . Here, writing 0x1122334455667788 to a variable holding 0 before could result in another thread reading 0x112233440000000 or 0x0000000055667788 . Now the specification does not mandate object references to be either int or

Why can't a load bypass a value written by another thread on the same core from a write buffer?

不羁岁月 提交于 2019-12-24 10:06:43
问题 If a CPU core uses a write buffer, then the load can bypass the most recent store to the referenced location from the write buffer, without waiting until it will appear in the cache. But, as it's written in A Primer on Memory Consistency and Coherence, if the CPU honors TSO memory model, then ... multithreading introduces a subtle write buffer issue for TSO. TSO write buffers are logically private to each thread context (virtual core). Thus, on a multithreaded core, one thread context should

Will a calling thread see modifications to local variables after thread.join()?

天大地大妈咪最大 提交于 2019-12-23 12:13:31
问题 In the simplest possible example, let's say I have a function that starts a thread, which in turn sets the value of a local variable to true. We join the thread, then leave the function. bool func() { bool b = false; std::thread t([&]() { b = true; }); t.join(); return b; } Will this function return true, or is the behavior undefined? 回答1: Yes, it must return true. [thread.thread.member] void join(); 4 Effects : Blocks until the thread represented by *this has completed. 5 Synchronization :

visibility of side effects when creating and joining threads

旧城冷巷雨未停 提交于 2019-12-23 12:07:34
问题 When are writes that are performed by one thread visible to a different thread when there are no synchronized blocks and no volatile variables? Here is a simplified quicksort example: int middle = partitionForTheFirstTime(array); Thread t = new Thread(new Quicksorter(array, 0, middle - 1)); Thread u = new Thread(new Quicksorter(array, middle + 1, array.size - 1)); t.start() u.start(); t.join(); u.join(); (For the sake of simplicity, assume that the two "worker threads" do not spawn any

Implicit synchronization when creating/joining threads

拥有回忆 提交于 2019-12-23 07:45:41
问题 What is the minimal framing required for x 's type for this code to work, considering the implied synchronization when creating/joining a thread: std::atomic ? volatile ? nothing? #include <thread> #include <cassert> int main() { int x = 123; // *** std::thread( [ & ] { assert( x == 123 ); x = 321; } ).join(); assert( x == 321 ); return 0; } 回答1: The invocation of std::thread 's constructor is synchronized and happens before the invocation of the copy of the thread function (30.3.1.2/6).

How does a Java virtual machine implement the “happens-before” memory model?

…衆ロ難τιáo~ 提交于 2019-12-22 05:58:22
问题 Java's memory model is based on "happens-before" relationship that enforces rules but also allows for optimization in the virtual machine's implementation in terms of cache invalidation. For example in the following case: // thread A private void method() { //code before lock synchronized (lockA) { //code inside } } // thread B private void method2() { //code before lock synchronized (lockA) { //code inside } } // thread B private void method3() { //code before lock synchronized (lockB) { /