atomic

Atomic load/store for OSs other than BSD?

Among the atomic operations provided by BSD (as given on the atomic(9) man page), there are atomic_load_acq_int() and atomic_store_rel_int(). In looking for the equivalent for other OSs (for example, by reading the atomic(3) man page for Mac OS X, the atomic_ops(3C) man page for Solaris, and the Interlocked*() functions for Windows), th...

How do I atomically read a value in x86 ASM?

I know how to atomically write a value in x86 ASM. But how do I read one? The LOCK prefix can't be used with mov. To increase a value, I am doing: lock inc dword ptr Counter How do I read Counter in a thread-safe way? ...

How do I transactionally poll a database queue table with rollback?

I wish to setup a database table that will be used as a message queue. The table will have messages inserted into it with a unique id and a timestamp and a status of 'PENDING'. Assuming that the insertion into this table is properly handled, I wish to know what is the best way to transactionally process messages from this table using a ...

java atomics operations

Is correct to divide the following statements: int v = ++j; as: read j value (atomic); increment by 1 the value read (NON atomic possibly interference by other thread); write the adding result to i (atomic); write i into v (atomic) ...

java synchronized on method Not working?

I'm experimenting Java Multi-Threading using synchronization on method comparing with Atomic variables (java.util.concurrent.atomic package). Below are the classes: // Interface ICounter.java public interface ICounter { public void increment(); public void decrement(); public int value(); ...

Should I protect operations on primitive types with mutexes for being thread-safe in C++?

What is the best approach to achieve thread-safety for rather simple operations? Consider a pair of functions: void setVal(int val) { this->_val = val; } int getVal() { return this->_val; } Since even assignments of primitive types aren't guaranteed to be atomic, should I modify every getter and setter in the program in th...

Is return atomic and should I use temporary in getter to be thread safe?

Is it necessary to use a temporary here to be thread-safe? int getVal() { this->_mutex.lock(); int result = this->_val; this->_mutex.unlock(); return result; } I'll give you disassembly of simple RAII test function int test() { RAIITest raii; //let's say it's a scoped lock return 3; } { 0...

Adding objects to queue without interruption

I would like to put two objects into a queue, but I've got to be sure the objects are in both queues at the same time, therefore it should not be interrupted in between - something like an atomic block. Does some one have a solution? Many thanks... queue_01.put(car) queue_02.put(bike) ...

Is the AutoResetEvent type an appropriate choice for an atomic switch?

Suppose I am processing a large amount of incoming data from multiple threads. I may want for this data to act as a trigger for a specific action when certain criteria are met. However, the action is not reentrant; so the same trigger being fired twice in rapid succession should only cause the action to run once. Using a simple bool fla...

Avoiding sleep while holding a spinlock

Hello! I've recently read section 5.5.2 (Spinlocks and Atomic Context) of LDDv3 book: Avoiding sleep while holding a lock can be more difficult; many kernel functions can sleep, and this behavior is not always well documented. Copying data to or from user space is an obvious example: the required user-space page may need to be swapp...

Why doesn't this code demonstrate the non-atomicity of reads/writes?

Reading this question, I wanted to test if I could demonstrate the non-atomicity of reads and writes on a type for which the atomicity of such operations is not guaranteed. private static double _d; [STAThread] static void Main() { new Thread(KeepMutating).Start(); KeepReading(); } private static void KeepReading() { whil...

Is python's shutil.move() atomic on linux ?

Hi, I am wondering whether python's shutil.move is atomic on linux ? Is the behavior different if the source and destination files are on two different partitions or is it same as when they are present on the same partition ? I am more concerned to know whether the shutil.move is atomic if the source and destination files are on the sa...

Implementing atomic<T>::store

I'm attempting to implement the atomic library from the C++0x draft. Specifically, I'm implementing §29.6/8, the store method: template <typename T> void atomic<T>::store(T pDesired, memory_order pOrder = memory_order_seq_cst); The requirement states: The order argument shall not be memory_order_consume, memory_order_acquire, nor...

Memory Mapped files and atomic writes of single blocks

If I read and write a single file using normal IO APIs, writes are guaranteed to be atomic on a per-block basis. That is, if my write only modifies a single block, the operating system guarantees that either the whole block is written, or nothing at all. How do I achieve the same effect on a memory mapped file? Memory mapped files are ...

Rsync operations

Hi, I'm trying to figure out how if rsyncing files is atomic. I couldn't find any confirmation about it. Due to rsync being able to send only deltas, I was under the impression that it also updates only parts of the live files. On the other hang DJB recommends rsync for synchronising live .cdb files and I've found this post ( http://lis...

Why doesn't the OpenMP atomic directive support assignment?

The atomic directive in openmp supports stuff like x += expr x *= expr where expr is an expression of scalar type that does not reference x. I get that, but I don't get why you can't do: #pragma omp atomic x = y; Is this somehow more taxing cpu instruction-wise? Seems to me that both the legal and illegal statement loads the value ...

C# - how to make a sequence of method calls atomic?

I have to make a sequence of method calls in C# such that, if one of them fails, the subsequent methods should not be called. In short, the set of calls should be made atomic. How do I achieve this in C#? ...

SQL atomic increment and locking strategies - is this safe?

I have a question about SQL and locking strategies. As an example, suppose I have a view counter for the images on my website. If I have a sproc or similar to perform the following statements: START TRANSACTION; UPDATE images SET counter=counter+1 WHERE image_id=some_parameter; COMMIT; Assume that the counter for a specific image_id h...

Is the return value of Interlocked.Exchange also processed atomically?

In the following code: a = Interlocked.Exchange(ref b, c); I know b is set to c atomically. But is a also set to b in the same atomic operation? Or is this outside of the atomic operation. What I need is to ensure both a and b are set in the same atomic operation. c => b, b => a This is in C#.Net. ...

How do I atomically swap 2 ints in C#?

What (if any) is the C# equivalent of the ASM command "XCHG". With that command, which imo is a genuine exchange (unlike Interlocked.Exchange), I could simply atomically swap two ints, which is what I am really trying to do. ...