You can use a shared Mutex to synchronize between multiple processes. This allows you to "lock" and provide a guard around the code which accesses the registry, if that is the shared storage mechanism you choose to use....

First of all, your assumption is false, volatile is not useless, it is just not sufficient, which is a different thing. The interfaces that you cite, must have volatile qualification, since otherwise they wouldn't work for objects that are such qualified. Now volatile is a difficult subject for C11, because...

Apologies for the lack of clarity with the question. After further reading, indeed I can use transactions however the area I was struggling to understand was that I need separate out the read from the update, and just wrap the update in the transaction along with usinf WATCH on the...

The definition of the volatile qualifier is given in the programming guide. It instructs the compiler to always generate a read or write for that access, and never "optimize" it into a register or some other optimization. Since atomic operations are guaranteed to act on actual memory locations (either shared...

If you pass a++ as the third argument, by the time the function call begins, a holds the incremented value (see order of evaluation ). Since that value no longer equals the value of the atomic, the C++ CAS reads from the atomic and writes to the second argument, which...

Why is the assembly output of store_idx_x86() the same as store_idx() and load_idx_x86() the same as load_idx()? On x86, assuming compiler-enforced alignment, they are the same operations. Loads and Stores to aligned addresses of the native size or smaller are guaranteed to be atomic. Reference Intel manual vol 3A,...

If you only ever use it as simple as this, you're fine. The problems start when: You add a condition - most conditions are fine, but avoid filtering based on Counter, that's a great way to lose determinism You update inside of a transaction (careful about this - it's easy...

According to the GCC Wiki this should be safe as the aquire/release memory model guarantees that the store to data in thread 1 will happen-before the atomic store to flag as data is not an atomic variable. "Any store before an atomic operation must be seen in other threads that...

Method 1: Build a set of "transactions". Since you only have one update per thread, you can easily build a fixed size "transaction" record, one entry per thread. Suppose I have 8 threads (for simplicity of presentation) and some arbitrary number of entries in my out table. Let's suppose my...

The operation provided by __sync_val_compare_and_swap can always be implemented in terms of __sync_bool_compare_and_swap (and of course the other direction is obviously possible), so in terms of power the two are equivalent. However implementing __sync_val_compare_and_swap in terms of __sync_bool_compare_and_swap is not very efficient. It looks something like: for (;;) { bool...

The shared timed mutex There is no equivalent for that kind of read-write locking in the C++11 standard library. The good news is that there is one in C++14 and it's called shared_timed_mutex. Take a look here: http://en.cppreference.com/w/cpp/thread/shared_timed_mutex Compiler support GCC's recent versions support shared_timed_mutex according to its documentation if...

First of all, I would use something other than a boolean, e.g. the process id of the process currently using the name. That way it becomes easier to debug and release bogus flag=true locks resulting from a process crash. Derby supports transactions which satisfy the ACID properties. A transaction can...

As I understand it (mainly from reading Jeff Preshings blog), an atomic_thread_fence(std::memory_order_acq_rel) prevents any reorderings except for StoreLoad, i.e., it still allows to reorder a Store with a subsequent Load. However, this is exactly the reordering that has to be prevented in your example. More precisely, an atomic_thread_fence(std::memory_order_acquire) prevents the...

The caution is intended for people who are using mapped pinned memory to coordinate execution between the CPU and GPU, or between multiple GPUs. When I wrote that, I did not expect anyone to use such a mechanism in the single-GPU case because CUDA provides so many other, better ways...

Usually, the atomicity of reading and writing a storage location is the same. That is to say, if a location cannot be written atomically, it also cannot be read atomically and vice versa. If a special atomic write is required, it is meaningless to use it unless the reads are...

You can't reassign references, but you can use pointers. (Would have commented but don't have the rep.) However, since an std::atomic is (most likely, but depends on your architecture) useful because of the way it funnels all access through atomic member functions, not because of any extra member data that...

Correct implementation of dispatch method in THREADPOOL should provide happens-before relation between all operations executed by the caller before this method call and all operations executed by the function(lambda in your case), passed to the method. So, auxiliary thread, executed your lambda function, will definitely see value of B, assigned...

t3, unlike the two other threads, does not perform an atomic add. Instead, it atomically loads a, performs the arithmetic (add 1) on a temporary, and atomically stores that new value back to a. This overwrites a regardless of atomic operations that might have happened in between. So you can...

You cannot use std::atomic with user-defined types that are not trivially copyable, and the Standard only provides a limited set of specializations for certain fundamental types. Here you can find the list of all the standard specializations of std::atomic. One approach you may want to consider is to write a...

compare_exchange_weak changes the first parameter to what was actually there. So after the first iteration of the loop expected has been changed to true. This then is fed back into the loop where the compare_exchange_weak succeeds and proceeds to fail the assertion.

The first barrier only applies during compilation. Once the compiler is done, it has no impact since nothing is added to the code. This could be useful to avoid some memory ordering issues (the compiler doesn't know how other threads may manipulate these memory locations, although hardly any compiler with...

No. Your code is not thread-safe. The AtomicBoolean methods themselves are thread-safe and avoid race conditions, but between the calls to those methods there can still be other threads accessing the object. In your case, first tokenValid.get() is called, and then tokenValid.getAndSet(boolean) is called. Between those two calls other threads...

If operations after a load could be moved to before the load, the assert(y == 1) in thread 2 could occur before the y = 1 in thread 1, even though (by rule 1) the assignment happens before the store. Only the two rules together ensure that the two statements...

Is it guaranteed that the effects of d = value; will not be seen before effects of a = version;, and will be seen before the effects of b = version;? Yes, it is. This is because sequensial consistency barrier is implied when read or write atomic<> variable. Instead...

Your Group.increment_counter call sends SQL like this to the database: update groups set tasks_count = coalesce(tasks_counter, 0) + 1 where id = X where X is @task.id. The SQL way to get the new tasks_counter value is to include a RETURNING clause: update groups set tasks_count = coalesce(tasks_counter, 0) +...

Does the strexeq clear the monitor set in the ldrex even if the condition is not equal? No. Nor does it need to - this is the "cmp" part of the cmpxchg - if the value loaded isn't the expected one, then the teq gives the ne condition, nothing...

No, this is not portable in general. calloc only guarantees a byte-wise 0 value of the underlying object. For types that (may) have a state this is not equivalent to an initialization. You definitively have to use atomic_init to put your object into a valid state. The reason for this...

In SWI Prolog (just not sure about ANSI) the atomic(X) predicate can tell you whether the X is list or a number (in your case). It can be used write the sum predicate by recursively calling itself when a list is encountered, or just add the element to a sum...

if(a + 4 < another_variable) // ... This is the best you can get with a single atomic. You are data-race free, as the reading of the atomic is safe against concurrent writes, and all subsequent operations happen on a copy of the original atomic value. A more verbose but...

The canonical way to handle the race condition you mention is by using an atomic compare-and-swap operation, which is supported on CUDA capable GPUs for both shared and global memory See atomicCAS in CUDA programming guide.. __shared__ int Checker; int TID = blockDim.x * blockIdx.x + threadId int localChecker; //...

I am not sure why is that and I suppose that the two threads are changing the counter at the same time, but I am not sure how it happens really because ++ is just a very simple instruction. Each thread is pulling the value of sum into a...

The function factor takes it's ThreadsCounter parameter by value, and std::atomic is not copy constructable. Eeven though you bound a reference to your thread function, it's attempting to create a copy to pass the function....

Yes, it can happen that two threads execute the function singular_process() at the same time. You can avoid this problem with compare_exchange: bool expected = false; if (executing_singular_process.compare_exchange_strong(expected, true)) { singular_process(); executing_singular_process = false; } ...

Django's transaction.atomic() is a thin abstraction over the transaction facilities of the database. So its behavior really depends on the database layer, which in turn depends on which database it is and what transaction isolation level is being used. So to really understand how this works, read and understand the...

There would be a difference if you reassign the variable: atomicInteger = new AtomicInteger() in a method, in which case marking the variable as volatile will guarantee that the assignment will be visible by other threads. But if you only use the AtomicInteger instance that is created with each instance...

The JNI does not provide any way for a native thread to get the address of a Java variable. All access to the variable, whether from Java bytecode or from a native thread, must go through JVM machinery. So your question really is moot. Java atomics "require atomicity with respect...

The main reason for mutual exclusion (i.e., what a Java synchronized block provides) is to prevent other threads from seeing data in an inconsistent state while one thread is changing the data. In order for it to work, all threads that access the data have to synchronize on the same...

That isn't a function but a member declaration with a non-static data member initializer. atomic<ListItem*> next{nullptr}; In a constructor that does not initialize next, next will automatically be initialized with nullptr. The initialization is done via list-initialization which was introduced in C++11 (together with non-static data member initializers - the...

It so appears that __sync_swap is simply an old fashioned name for what can be achieved with more "up to date" built-ins. Lets consider a case in point (atomic macros as implemented by freebsd: http://code.metager.de/source/xref/freebsd/sys/sys/stdatomic.h): #if defined(__CLANG_ATOMICS) .... #define atomic_exchange_explicit(object, desired, order) \ __c11_atomic_exchange(object, desired, order) .... #elif defined(__GNUC_ATOMICS) .......

Your code gives me rather more error messages than the fragment you've posted. The most relevant are error: cannot convert ‘B’ to ‘B*’ in initialisation note: no known conversion for argument 1 from ‘B*’ to ‘B&’ indicating that you're declaring a pointer when you want an object: B b2 =...

using a AtomicBoolean all read and write actions are atomic (as the name includes). with a volatile boolean you still have to deal with race conditions, when two threads accessing the variable at the same time.

No, Torn reads are possible. Assume your field access is reading the data and interleaved partway by the Interlocked.Exchange, then other 32 bits will be the updated value of Exchange and thus produces torn read. For atomic read, you need to use Interlocked.Read (in 32 bit machines). The Read method...

As long as your used memory order is at least acquire/release (which is the default), you are guaranteed to see all updates (not just the ones to atomic variables) the writing thread did before setting the flag to true as soon as you can read the write. So yes, this...

According to http://en.cppreference.com/w/cpp/memory/shared_ptr: yes. my_ptr = dataPtr is thread-safe. All member functions (including copy constructor and copy assignment) can be called by multiple threads on different instances of shared_ptr without additional synchronization even if these instances are copies and share ownership of the same object. There is no guarantee though...

Sure, but you just have to call it in a loop. On each loop iteration, read the old value. If it's null, break out of the loop and return failure. If it's not null, use that old value as the "expected" for atomic compare-and-exchange. If it succeeds, exit the loop...

It has been mentioned that your approach is risky business. Here is what you may want to consider instead: Use std::shared_ptr<const std::string> with immutable values, and the shared_ptr atomic_load and atomic_store. std::shared_ptr will ensure you do not access a dangling pointer, while the immutability (string does not change after construction)...

The sequenced-before (not the same as happens-before) relationships are not specific to multithreading. They happen in single threaded programs as well. Any expression that ends with a semicolon is sequenced-before the next, so in this case A is sequenced before B and C before D because each of them is...

You can indeed just declare it as std::atomic<int>, and things should work as you want. volatile is about preserving the sequence of addresses and values that the generated code must present to the processor for reading/writing. It doesn't at all constrain what the hardware does with that for purposes of...

Why the first approach doesn't work for you? Unless you intend to perform atomic update on the whole array, it is the way to go. I compiled the following code successfully: struct node { unsigned long key; tbb::atomic<struct node*> childrenArray[2]; }; void main() { node n; n.childrenArray[0].compare_and_swap(0,0); } ...

Having found no arguments to prove my understanding wrong I deem it correct and my proof is as follows: memory_order_seq_cst - T2 load is obliged to load 2. That's correct because all operations using memory_order_seq_cst should form the single total order on the atomic variable of all the memory operations....

You need to read The Go Memory Model You'll discover that Go has nothing like the control that you have in C++ - there isn't a direct translation of the C++ features in your post. This is a deliberate design decision by the Go authors - the Go motto is...

If only one value is not NA amongst IMILEFT and IMIRIGHT (as in your example), just try (df is your data.frame): indx<-is.na(df$IMIAVG) df$IMIAVG[indx]<-rowSums(df[indx,1:2],na.rm=TRUE) Btw, if you want to find the mean value of each row and exclude the NA values in the process, you can set the na.rm argument as...

The first and most important problem: this can't possibly work. You need synchronization around the execution of the member functions, not around retrieving the list. std::atomic doesn't even begin to resemble what you need. Regarding your attempted implementation, casting an atomic<T> to T& can't do anything reasonable. And even if...

Is my code right? Yes. You application of Double-Checked Locking Pattern is correct. But see below for some improvements. Does it performance better ? When compared with fully locked variant(2nd in your post), it mostly has better performance, until magicValue() is called only once(but even in that case performance...

Sigh, this was too long for a comment: Isn't the meaning of atomic "to appear to occur instantaneously to the rest of the system"? I'd say yes and no to that one, depending on how you think of it. For writes with SEQ_CST, yes. But as far as how atomic...

First of all, you are perfectly allowed to have something like std::atomic<very_nontrivial_large_structure>, so std::atomic as such cannot generally be guaranteed to be lock-free (although most specializations for trivial types like bool or int probably could, on most systems). But that is somewhat unrelated. The exact reasoning why atomic_flag and nothing...

It's nonsense. Either one may or may not be atomic, depending on the data type, the architecture, and perhaps the compiler (the standard doesn't make guarantees about atomicity in general unless you're using C11 atomics), but I can't see any good reason to think that, in general, i++ would be...

The problem here isn't so much with Clojure, but with the repl you are using. The call to swap! is working just fine, it's that the repl is attempting to print out the result and it can't since the sequence never ends. You can set the number of items printed...

std::atomic<.> can only be used with a 'trivially copyable type'. std::shared_ptr<std::string> obviously doesn't satisfy those criteria. Somewhere atomic will be copying the object as a block of memory and violating some invariant of one or more of the classes. For example we all know that: std::shared_ptr<std::string> s(new std::string("abc")); std::shared_ptr<std::string> t;...

std::atomic<double> is supported in the sense that you can create one in your program and it will work under the rules of C++11. You can perform loads and stores with it and do compare-exchange and the like. The standard specifies that arithmetic operations (+, *, +=, &, etc.) are only...

According to the source file at https://github.com/julianstorer/JUCE/blob/master/modules/juce_core/memory/juce_Atomic.h, the implementation of Atomic class in JUCE uses compiler intrinsics or platform-specific calls that are equivalent to atomic operations with memory_order_seq_cst ordering in C++11.

Since you tagged x86, this will be x86-specific. An increment is essentially three parts, read, add, write. The increment is not atomic, but all three steps (the add doesn't count I suppose, it's not observable anyway) of it are as long as the variable does not cross a cache line...

Yes, this should work as long as you consistently use select_for_update() before modifying anything that could affect your availability check. This code will not interfere with itself, since the select_for_update() will acquire row-level locks on the key rows at the beginning. But let's say you have another view that allows...

Is there a reason why std::atomic isn't copy-constructable? Yes. When you are asking for a copy constructible atomic, you're asking for the "normal" rules of single-threaded sequential consistency to apply to a variable that doesn't follow those rules. In essence, there is no generalized solution. By using the constructor you...

OpenMP atomics need to operate on a scalar value (simple type). Atomics are intended to be mapped to kernel or even instruction level atomics by the runtime. Without knowing more about your problem, it is hard to give one answer, but some usual suggestions for this sort of thing: Use...

this [workaround] doesn't seem very elegant. std::atomic<T> cannot make arbitrary operations atomic: only loading and storing the data is supported. That is why your "workaround" is actually the way to deal with atomic objects: you prepare the new node value in any way that you like, and then atomically...

If you want to do two different atomic operations together you need a mutex both when you write and when you read them. Mutexes don't prevent context switches, but used this way guarantee that you don't access half updated states. Alternatively, if you really don't want to use a mutex,...

Could someone show me, using the C++ standard notions, that the code is perfectly safe? I initially had the same concerns as you. I think the key is understanding that operations on the std::atomic_flag variable are atomic with respect to all processors/cores. Two atomic 'test and set' operations in...

Oh boy. Let's assume that each process has access to a unique identifier, to avoid problems breaking symmetry. Here's a wait-free implementation of a one-shot consensus object. Create a directory with a unique name. Create a file in that directory whose name is the creating process's input. Rename the directory...

You can try to implement a multi-word CAS using the available CAS, however this is dependent on how much of a performance hit you are willing to take in order to achieve this. You can look at Harris' http://www.cl.cam.ac.uk/research/srg/netos/papers/2002-casn.pdf...

There is to my knowledge nothing built in. There are modules such as redis-lock though, that implement a lock mechanism. If you run on a single non-clustered server you could probably cope with implementing a simple local lock though.

Très Bon, Your both right, although in fairness to all Tommy reply was the one that lead me to the fix. for (NSString *student in studentNClass) { if ([descriptions containsObject:student] ) { [studentNClass removeObjectForKey:student]; } else { [self addBeacon2Carousel:student]; //singleBeaconsFound++; self.beaconsFound.text = [NSString stringWithFormat:@"%d",singleBeaconsFound];; } After Tommy.. for (NSString *student...