AtomicInteger lazySet vs. set

Cited straight from “JDK-6275329: Add lazySet methods to atomic classes”: As probably the last little JSR166 follow-up for Mustang, we added a “lazySet” method to the Atomic classes (AtomicInteger, AtomicReference, etc). This is a niche method that is sometimes useful when fine-tuning code using non-blocking data structures. The semantics are that the write is guaranteed … Read more

When to use volatile with multi threading?

Short & quick answer: volatile is (nearly) useless for platform-agnostic, multithreaded application programming. It does not provide any synchronization, it does not create memory fences, nor does it ensure the order of execution of operations. It does not make operations atomic. It does not make your code magically thread safe. volatile may be the single-most … Read more

What exactly is std::atomic?

Each instantiation and full specialization of std::atomic<> represents a type that different threads can simultaneously operate on (their instances), without raising undefined behavior: Objects of atomic types are the only C++ objects that are free from data races; that is, if one thread writes to an atomic object while another thread reads from it, the … Read more

Practical uses for AtomicInteger

There are two main uses of AtomicInteger: As an atomic counter (incrementAndGet(), etc) that can be used by many threads concurrently As a primitive that supports compare-and-swap instruction (compareAndSet()) to implement non-blocking algorithms. Here is an example of non-blocking random number generator from Brian Göetz’s Java Concurrency In Practice: public class AtomicPseudoRandom extends PseudoRandom { … Read more

What does “atomic” mean in programming?

Here’s an example: Suppose foo is a variable of type long, then the following operation is not an atomic operation (in Java): foo = 65465498L; Indeed, the variable is written using two separate operations: one that writes the first 32 bits, and a second one which writes the last 32 bits. That means that another … Read more

What is the difference between atomic / volatile / synchronized?

You are specifically asking about how they internally work, so here you are: No synchronization private int counter; public int getNextUniqueIndex() { return counter++; } It basically reads value from memory, increments it and puts back to memory. This works in single thread but nowadays, in the era of multi-core, multi-CPU, multi-level caches it won’t … Read more

What’s the difference between the atomic and nonatomic attributes?

The last two are identical; “atomic” is the default behavior (note that it is not actually a keyword; it is specified only by the absence of nonatomic — atomic was added as a keyword in recent versions of llvm/clang). Assuming that you are @synthesizing the method implementations, atomic vs. non-atomic changes the generated code. If … Read more

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