memory-model

Does this example contain a data race?

馋奶兔 提交于 2019-12-06 03:54:41
Here is the originan question, but mine have some differences with it. C++ memory model - does this example contain a data race? My question: //CODE-1: initially, x == 0 and y == 0 if (x) y++; // pthread 1 if (y) x++; // pthread 2 Note: the code above is written in C, not C++ (without a memory model). So does it contain a data race? From my point of view: if we view the code in Sequential Consistency memory model, there is no data race because x and y will never be both non-zero at the same time. However, we can never assume a Sequential Consistency memory model, so the compilier reordering

What does “strongly happens before” mean?

做~自己de王妃 提交于 2019-12-05 22:09:00
问题 The phrase "strongly happens before" is used several times in the C++ draft standard. For example: Termination [basic.start.term]/5 If the completion of the initialization of an object with static storage duration strongly happens before a call to std​::​atexit (see , [support.start.term]), the call to the function passed to std​::​atexit is sequenced before the call to the destructor for the object. If a call to std​::​atexit strongly happens before the completion of the initialization of an

Java Memory Model: Is it safe to create a cyclical reference graph of final instance fields, all assigned within the same thread?

寵の児 提交于 2019-12-05 13:14:25
问题 Can somebody who understand the Java Memory Model better than me confirm my understanding that the following code is correctly synchronized? class Foo { private final Bar bar; Foo() { this.bar = new Bar(this); } } class Bar { private final Foo foo; Bar(Foo foo) { this.foo = foo; } } I understand that this code is correct but I haven't worked through the whole happens-before math. I did find two informal quotations that suggest this is lawful, though I'm a bit wary of completely relying on

Atomic pointers in c++ and passing objects between threads

一曲冷凌霜 提交于 2019-12-05 08:18:59
My question involves std::atomic and the data that this pointer points to. If in thread 1 I have Object A; std:atomic<Object*> ptr; int bar = 2; A.foo = 4; //foo is an int; ptr.store(*A); and if in thread 2 I observe that ptr points to A, can I be guaranteed that ptr->foo is 4 and bar is 2? Does the default memory model for the atomic pointer (Sequentially consistent) guarantee that assignments on non atomic (in this case A.foo) that happen before an atomic store will be seen by other threads before it sees the assignment of the same atomic.store for both cases? If it helps or matters, I am

std::atomic<int> memory_order_relaxed VS volatile sig_atomic_t in a multithreaded program

谁说我不能喝 提交于 2019-12-05 08:08:04
Does volatile sig_atomic_t give any memory order guarantees? E.g. if I need to just load/store an integer is it ok to use? E.g. here: volatile sig_atomic_t x = 0; ... void f() { std::thread t([&] {x = 1;}); while(x != 1) {/*waiting...*/} //done! } is it correct code? Are there conditions it may not work? Note: This is a over-simplifed example, i.e. I am not looking for a better solution for the given piece of code. I just want to understand what kind of behaviour I could expect from volatile sig_atomic_t in a multithreaded program according to the C++ standard. Or, if it is a case, understand

Is it possible to have a release-sequence from a release store operation to a store in a different thread?

假如想象 提交于 2019-12-05 07:18:50
I'm aware that a synchronizes-with relationship will occur between a release store operation in thread 2 and a acquire load operation in thread 1 even if that load operation isn't directly reading the value stored by thread 2, provided that there is a "release sequence" between the release store operation and the store that is actually being read as long as: The store that is actually being read is in the same thread as the release store operation. In modification-order there is no store in other threads between the release store operation and the store that is actually being read (read-modify

Does a lock around a write guarantee fresh read in another thread? (.Net, memory model)

十年热恋 提交于 2019-12-05 02:12:33
问题 Say I have a property whose setter is protected by a lock, but without any lock around the getter, e.g. private long _myField; public long MyProperty { get { return _myField; } set { lock(whatever) _myField = value; } } In addition to synchronizing writes (but not reads), the lock, or rather Monitor.Exit, should cause a volatile write. Let's now say we have two threads A and B, and the following sequence happens: A reads the current value of MyProperty . B writes a new value to MyProperty . A

C++ memory model - does this example contain a data race?

一个人想着一个人 提交于 2019-12-05 01:42:22
I was reading Bjarne Stroustrup's C++11 FAQ and I'm having trouble understanding an example in the memory model section. He gives the following code snippet: // start with x==0 and y==0 if (x) y = 1; // thread 1 if (y) x = 1; // thread 2 The FAQ says there is not a data race here. I don't understand. The memory location x is read by thread 1 and written to by thread 2 without any synchronization (and the same goes for y ). That's two accesses, one of which is a write. Isn't that the definition of a data race? Further, it says that "every current C++ compiler (that I know of) gives the one

Memory ordering restrictions on x86 architecture

隐身守侯 提交于 2019-12-04 17:18:54
In his great book 'C++ Concurrency in Action' Anthony Williams writes the following (page 309): For example, on x86 and x86-64 architectures, atomic load operations are always the same, whether tagged memory_order_relaxed or memory_order_seq_cst (see section 5.3.3). This means that code written using relaxed memory ordering may work on systems with an x86 architecture, where it would fail on a system with a finer- grained set of memory-ordering instructions such as SPARC. Do I get this right that on x86 architecture all atomic load operations are memory_order_seq_cst ? In addition, on the

String constants vs char arrays in C [duplicate]

夙愿已清 提交于 2019-12-04 17:12:55
This question already has answers here : Closed 8 years ago . Possible Duplicate: What is the difference between char s[] and char *s in C? More of a general question rather than trying to fix something, I've been reading the C programming language book and they take care to make the distinction between char amessage[] = "blah"; char *pmessage = "blah"; The difference being one is a char array and the other a pointer to a string constant. They say modifying the char array is acceptable but you shouldn't modify string constants as it triggers undefined behavior. My question is: isn't the string