atomic

How to guarantee 64-bit writes are atomic?

When can 64-bit writes be guaranteed to be atomic, when programming in C on an Intel x86-based platform (in particular, an Intel-based Mac running MacOSX 10.4 using the Intel compiler)? For example: unsigned long long int y; y = 0xfedcba87654321ULL; /* ... a bunch of other time-consuming stuff happens... */ y = 0x12345678abcdefULL; I...

How to implement thread safe reference counting in C++

How do you implement an efficient and thread safe reference counting system on X86 CPUs in the C++ programming language? I always run into the problem that the critical operations not atomic, and the available X86 Interlock operations are not sufficient for implementing the ref counting system. The following article covers this topic,...

Link error when compiling gcc atomic operation in 32-bit mode

I have the following program: ~/test> cat test.cc int main() { int i = 3; int j = __sync_add_and_fetch(&i, 1); return 0; } I'm compiling this program using GCC 4.2.2 on Linux running on a multi-cpu 64-bit Intel machine: ~/test> uname --all Linux doom 2.6.9-67.ELsmp #1 SMP Wed Nov 7 13:56:44 EST 2007 x86_64 x86_64 x86_64 GNU/Lin...

Interlocked equivalent on Linux

In a C++ Linux app, what is the simplest way to get the functionality that the Interlocked functions on Win32 provide? Specifically, a lightweight way to atomically increment or add 32 or 64 bit integers? ...

Which CPU architectures support Compare And Swap (CAS)?

Hi, just curious to know which CPU architectures support compare and swap atomic primitives? ...

High-level Compare And Swap (CAS) functions?

I'd like to document what high-level (i.e. C++ not inline assembler ) functions or macros are available for Compare And Swap (CAS) atomic primitives... E.g., WIN32 on x86 has a family of functions _InterlockedCompareExchange in the <_intrin.h> header. ...

Thread-safe atomic operations in gcc

In a program I work on, I have a lot of code as follows: pthread_mutex_lock( &frame->mutex ); frame->variable = variable; pthread_mutex_unlock( &frame->mutex ); This is clearly a waste of CPU cycles if the middle instruction can just be replaced with an atomic store. I know that gcc is quite capable of this, but I haven't been able t...

How do I make IEditableObject.EndEdit atomic?

If I have an Address class that implements IEditableObject, I might have EndEdit implementation like this: public void EndEdit() { // BeginEdit would have set _editInProgress and save to *Editing fields if (_editInProgress) { _line1 = _line1Editing; _line2 = _line2Editing; _city = _cityEditing; ...

How do I make IEditableObject.EndEdit atomic?

If I have an Address object which implements IEditableObject, I might have EndEdit implementation like this: public void EndEdit() { // BeginEdit would set _editInProgress and update *Editing fields; if (_editInProgress) { _line1 = _line1Editing; _line2 = _line2Editing; _city = _cityEditing; _...

Update more than one row atomically

I need to execute a select and then update some of the rows in the ResultSet in an atomic way. The code I am using looks like (simplified): stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery("SELECT ..."); while (rs.next()) { if (conditions_to_update) { rs.update...

Is this C++ implementation for an Atomic float safe?

Edit: The code here still has some bugs in it, and it could do better in the performance department, but instead of trying to fix this, for the record I took the problem over to the Intel discussion groups and got lots of great feedback, and if all goes well a polished version of Atomic float will be included in a near future release ...

Is SetEvent atomic?

Is it safe to have 2 or more threads call the Win32 API's SetEvent on the same event handler not being protected by a critical section? ...

Win32: Atomic Execution of Code Block

I have two system calls GetSystemTime() and GetThreadTimes() that I need to calculate the CPU utilization by a given Win32 thread. For the sake of accuracy, I need to ensure that both GetSystemTime() and GetThreadTimes() are executed atomically; i.e. there should be no context switch in between a call to GetSystemTime() & GetThreadTimes...

Moving a directory atomically

I have two directories in the same parent directory. Call the parent directory base and the children directories alpha and bravo. I want to replace alpha with bravo. The simplest method is: rm -rf alpha mv bravo alpha The mv command is atomic, but the rm -rf is not. Is there a simple way in bash to atomically replace alpha with br...

Django: How can I protect against concurrent modification of data base entries

If there a way to protect against concurrent modifications of the same data base entry by two or more users? It would be acceptable to show an error message to the user performing the second commit/save operation, but data should not be silently overwritten. I think locking the entry is not an option, as a user might use the "Back" but...

Can I atomically rename/replace 2 or more tables and views?

Given a table X and a view Y (that has the same structure as X) is there a way to rename X to Z and Y to X atomically so that no query will ever see nothing named X? Renaming X and creating the view would also be valid. The point would be to, as part of a schema migration plan, replace the old tables with views that simulate the old ver...

Is it possible to avoid a wakeup-waiting race using only POSIX semaphores? Is it benign?

I'd like to use POSIX semaphores to manage atomic get and put from a file representing a queue. I want the flexibility of having something named in the filesystem, so that completely unrelated processes can share a queue. I think this plan rules out pthreads. The named posix semaphores are great for putting something in the filesystem...

How can I ensure atomicity of a get-and-set operation to redirect Console.Out for logging console output?

I need to intercept the console output stream(s) in order to capture it for a log but still pass things through to the original stream so the application works properly. This obviously means storing the original Console.Out TextWriter before changing it with Console.SetOut(new MyTextWriterClass(originalOut)). I assume the individual op...

What's the best way to do a cross-platform, atomic file replacement in Perl?

I have a very common situation. I have a file, and I need to entirely overwrite that file with new contents. However, the original file is accessed on every page load (this is a web app), so it can't be missing for very long. A few ms is OK (though not ideal), a second is not OK. Right now I do this by writing a temp file to the same di...

How to do text full history in Django?

I'd like to have the full history of a large text field edited by users, stored using Django. I've seen the projects: Django Full History (Google Code) Django ModelHistory, and Django FullHistory I've a special use-case that probably falls outside the scope of what these projects provide. Further, I'm wary of how well documented, te...