How do “acquire” and “consume” memory orders differ, and when is “consume” preferable?

Data dependency ordering was introduced by N2492 with the following rationale: There are two significant use cases where the current working draft (N2461) does not support scalability near that possible on some existing hardware. read access to rarely written concurrent data structures Rarely written concurrent data structures are quite common, both in operating-system kernels and … Read more

How to make file creation an atomic operation?

Write data to a temporary file and when data has been successfully written, rename the file to the correct destination file e.g with open(tmpFile, ‘w’) as f: f.write(text) # make sure that all data is on disk # see http://stackoverflow.com/questions/7433057/is-rename-without-fsync-safe f.flush() os.fsync(f.fileno()) os.replace(tmpFile, myFile) # os.rename pre-3.3, but os.rename won’t work on Windows According to … Read more

How to implement multithread safe singleton in C++11 without using

C++11 removes the need for manual locking. Concurrent execution shall wait if a static local variable is already being initialized. ยง6.7 [stmt.dcl] p4 If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. As such, simple have a static function like this: static … Read more

Is id = 1 – id atomic?

Am I wrong? Nope, you’re absolutely right – as is your example timeline. In addition to it not being atomic, it’s not guaranteed that the write to id will be picked up by the other thread anyway, given that there’s no synchronization and the field isn’t volatile. It’s somewhat disconcerting for reference material like this … Read more

What are atomic operations for newbies?

Pretty much, yes. “Atom” comes from greek “atomos” = “uncuttable”, and has been used in the sense “indivisible smallest unit” for a very long time (till physicists found that, in fact, there are smaller things than atoms). In concurrent programming, it means that there will be no context switch during it – nothing can affect … Read more

Why is the volatile qualifier used through out std::atomic?

To summarize what others have correctly written: C/C++ volatile is for hardware access and interrupts. C++11 atomic<> is for inter-thread communication (e.g., in lock-free code). Those two concepts/uses are orthogonal, but they have overlapping requirements and that is why people have often confused the two. The reason that atomic<> has volatile-qualified functions is the same … Read more

Must I call atomic load/store explicitly?

Are assignment and access operations for non-reference types also atomic? Yes, they are. atomic<T>::operator T and atomic<T>::operator= are equivalent to atomic<T>::load and atomic<T>::store respectively. All the operators are implemented in the atomic class such that they will use atomic operations as you would expect. I’m not sure what you mean about “non-reference” types? Not sure … Read more

Django: How can I protect against concurrent modification of database entries

This is how I do optimistic locking in Django: updated = Entry.objects.filter(Q(id=e.id) && Q(version=e.version))\ .update(updated_field=new_value, version=e.version+1) if not updated: raise ConcurrentModificationException() The code listed above can be implemented as a method in Custom Manager. I am making the following assumptions: filter().update() will result in a single database query because filter is lazy a database query … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)