atomicity

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

你。 提交于 2019-12-04 08:23:36
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 eventual consistency with respect to other relaxed memory loads and/or compare_exchange s? Using std:

Memory ordering behavior of std::atomic::load

浪尽此生 提交于 2019-12-04 07:02:31
Am I wrong to assume that the atomic::load should also act as a memory barrier ensuring that all previous non-atomic writes will become visible by other threads? To illustrate: volatile bool arm1 = false; std::atomic_bool arm2 = false; bool triggered = false; Thread1: arm1 = true; //std::std::atomic_thread_fence(std::memory_order_seq_cst); // this would do the trick if (arm2.load()) triggered = true; Thread2: arm2.store(true); if (arm1) triggered = true; I expected that after executing both 'triggered' would be true. Please don't suggest to make arm1 atomic, the point is to explore the

What does AtomicReference.compareAndSet() use for determination?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 07:01:56
Say you have the following class public class AccessStatistics { private final int noPages, noErrors; public AccessStatistics(int noPages, int noErrors) { this.noPages = noPages; this.noErrors = noErrors; } public int getNoPages() { return noPages; } public int getNoErrors() { return noErrors; } } and you execute the following code private AtomicReference<AccessStatistics> stats = new AtomicReference<AccessStatistics>(new AccessStatistics(0, 0)); public void incrementPageCount(boolean wasError) { AccessStatistics prev, newValue; do { prev = stats.get(); int noPages = prev.getNoPages() + 1; int

python - ensure script is activated only once

北慕城南 提交于 2019-12-04 05:54:54
I'm writing a Python 2.7 script. In summary, this script is being run every night on Linux and activates several processes. I'd like to ensure this script is not run multiple times in parallel (basically trying to mimic Singleton pattern but on application level) . Code Example def main(): # before doing anything, I'd like to know whether this # script was activated and alive. # if so, error out # do something if __name__ == "__main__": main() Suggestion The naive solution would be to create some kind of a lock file, that acts as a mutex. The first thing we do is to check whether this file

Why is the << operation on an array in Ruby not atomic?

。_饼干妹妹 提交于 2019-12-03 23:49:13
问题 In Ruby, this code is not threadsafe if array is modified by many threads: array = [] array << :foo # many threads can run this code Why is the << operation not thread safe? 回答1: array is your program variable when you apply an operation like << to it. It happens in three-steps: The variable is first copied into a CPU register. The CPU performs computations. The CPU writes back the result to variable memory. So this high-level single-operation is performed in three steps. In between these

usage golang atomic LoadInt32/StoreInt32 (64)

江枫思渺然 提交于 2019-12-03 22:14:26
问题 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) 回答1: 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,

is sql server transaction atomic

£可爱£侵袭症+ 提交于 2019-12-03 14:53:48
so I have a stored procedure (sql server 2008 r2) something like this BEGIN TRAN BEGIN TRY //critical section select value update value //end of critical section COMMIT END TRY BEGIN CATCH IF @@TRANCOUNT > 0 ROLLBACK END CATCH I want no two stored procedures read the same value. In other words read and update should be atomic. This code does this? If not how do I do it? Yes they are atomic but that does not mean that you will get the behaviour that you want here! The property you need to look at is isolation. To achieve the exclusion that you require you would need to make the SELECT operation

What happens if I log into the same file from multiple different processes in python?

安稳与你 提交于 2019-12-03 14:24:39
I spent hours to dig the behavior, first about those questions: Atomicity of `write(2)` to a local filesystem How can I synchronize -- make atomic -- writes on one file from from two processes? How does one programmatically determine if "write" system call is atomic on a particular file? What happens if a write system call is called on same file by 2 different processes simultaneously http://article.gmane.org/gmane.linux.kernel/43445 It seems if we use 'O_APPEND' flag when opening file, it will always be ok to logging to same file from multiple processes, on linux. And i believe python surely

Relation between bytecode instructions and processor operations

人盡茶涼 提交于 2019-12-03 12:29:51
问题 Java specification guarantees primitive variable assignments are always atomic (expect for long and double types . On the contrary, Fetch-and-Add operation corresponding to the famous i++ increment operation, would be non-atomic because leading to a read-modify-write operation. Assuming this code: public void assign(int b) { int a = b; } The generated bytecode is: public void assign(int); Code: 0: iload_1 1: istore_2 2: return Thus, we see the assignment is composed of two steps (loading and

Is the “switch” statement evaluation thread-safe?

◇◆丶佛笑我妖孽 提交于 2019-12-03 10:47:54
问题 Consider the following sample code: class MyClass { public long x; public void DoWork() { switch (x) { case 0xFF00000000L: // do whatever... break; case 0xFFL: // do whatever... break; default: //notify that something going wrong throw new Exception(); } } } Forget the uselessness of the snippet: my doubt is about the behavior of the switch statement. Suppose that the x field could have only two values: 0xFF00000000L or 0xFFL . The switch above should not fall into the "default" option. Now