atomicity

Atomically creating a file if it doesn't exist in Python

蹲街弑〆低调 提交于 2019-12-01 02:58:39
问题 I am looking for an atomic version of the following: import os def tryMakeFile(filename): try: with open(filename) as _: return False except FileNotFoundError: with open(filename, mode='a') as _: return True (Please don't comment on stylistic issues here - I know this code is bad in many ways, but it suffices to illustrate my question.) In other words, I'm looking for a way to check if a file exists, and create it if it doesn't, in Python, in such a way that I know which happened. But done in

x86-64 usage of LFENCE

北城余情 提交于 2019-12-01 02:20:44
问题 I'm trying to understand the right way to use fences when measuring time with RDTSC/RDTSCP. Several questions on SO related to this have already been answered elaborately. I have gone through a few of them. I have also gone through this really helpful article on the same topic: http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/ia-32-ia-64-benchmark-code-execution-paper.pdf However, in another online blog, there's an example of using LFENCE instead of CPUID on x86. I was

usage golang atomic LoadInt32/StoreInt32 (64)

梦想的初衷 提交于 2019-11-30 23:18:07
Can anybody show the example where usage of such atomic operations needed. I don't understand a differenece between import "sync/atomic" ... var sharedA int64 var sharedB *int64 ... // concurent code tmpVarA := sharedA tmpVarB := *sharedB // and tmpVarA := atomic.LoadInt64(&sharedA) tmpVarB := atomic.LoadInt64(sharedB) It's not documented in the package at all, but normally atomic loads and stores of normal values are there not for atomicity because the CPU operations are already atomic, but for ordering. The language specification or CPU instruction documentation gives you certain guarantees

ARM: Is writing/reading from int atomic?

允我心安 提交于 2019-11-30 22:54:21
问题 On ARM architecture, unfortunately I don't know exactly what chip it is, is a 32 bit int read/write atomic? Is there any sort of guarantees about reads/writes to basic types? 回答1: It should be atomic, EXCEPT if that int is stored on a non-aligned address. 来源: https://stackoverflow.com/questions/9399026/arm-is-writing-reading-from-int-atomic

std::memory_order_relaxed atomicity with respect to the same atomic variable

僤鯓⒐⒋嵵緔 提交于 2019-11-30 17:35:31
问题 The cppreference documentation about memory orders says Typical use for relaxed memory ordering is incrementing counters, such as the reference counters of std::shared_ptr, since this only requires atomicity, but not ordering or synchronization ( note that decrementing the shared_ptr counters requires acquire-release synchronization with the destructor ) Does this mean that relaxed memory ordering don't actually result in atomicity with respect to the same variable? But rather just results in

Difference between getAndSet and compareAndSet in AtomicBoolean

旧街凉风 提交于 2019-11-30 16:42:54
问题 The thread title should be self-explnatory... I'm a bit confused between the specification of below methos from AtomicBoolean class: java.util.concurrent.atomic.AtomicBoolean#compareAndSet java.util.concurrent.atomic.AtomicBoolean#getAndSet My assemption is that both would result in the same behavior when used as a boolean clause in an if condition: public class Test { private AtomicBoolean flag = AtomicBoolean(false); public void processSomeAction() { if (flag.getAndSet(false)) { // Shouldn

Atomic Instruction

天大地大妈咪最大 提交于 2019-11-30 09:02:17
问题 What do you mean by Atomic instructions? How does the following become Atomic? TestAndSet int TestAndSet(int *x){ register int temp = *x; *x = 1; return temp; } From a software perspective, if one does not want to use non-blocking synchronization primitives, how can one ensure Atomicity of instruction? is it possible only at Hardware or some assembly level directive optimization can be used? 回答1: Some machine instructions are intrinsically atomic - for example, reading and writing properly

what is “failure atomicity” used by J bloch and how its beneficial in terms of immutable object?

非 Y 不嫁゛ 提交于 2019-11-30 03:38:35
just came across below statement as benefit of immutable object Immutable object always have “failure atomicity” (a term used by Joshua Bloch) : if an immutable object throws an exception, it’s never left in an undesirable or indeterminate state. can any one explain it in more detail and why is it so? Bloch's "Failure atomicity" means that if a method threw an exception, the object should still be usable afterwards. Generally, the object should be in the same state as it was before invoking the method. In the case of an immutable object, you gain that simply from the fact that it's immutable.

In C is “i+=1;” atomic?

狂风中的少年 提交于 2019-11-30 03:13:24
In C, is i+=1; atomic? The C standard does not define whether it is atomic or not. In practice, you never write code which fails if a given operation is atomic, but you might well write code which fails if it isn't . So assume it isn't. No. The only operation guaranteed by the C language standard to be atomic is assigning or retrieving a value to/from a variable of type sig_atomic_t , defined in <signal.h> . (C99, chapter 7.14 Signal handling.) Defined in C, no. In practice, maybe. Write it in assembly. The standard make no guarantees. Therefore a portable program would not make the assumption

AtomicBoolean vs Synchronized block, whats the difference

旧城冷巷雨未停 提交于 2019-11-29 14:15:28
I am trying to understand the difference between the two following code blocks AtomicBoolean ab = new AtomicBoolean(false); using the following to get and set state. . ab.get(); ab.set(X); vs. private boolean ab = false; private final Object myboollock = new Ojbect(); public void setAB(boolean state) { synchronized(myboollock) { ab = state; } } public boolean getAB() { synchronized(myboollock) { return ab; } } I need to thread protect a boolean, that is all, and have in the past used the later method, but would like to start to use Atomic objects, (if ) they are safe?, There are a few subtle