4,043 questions
1954
votes
28
answers
541k
views
What's the difference between the atomic and nonatomic attributes?
What do atomic and nonatomic mean in property declarations?
@property(nonatomic, retain) UITextField *userName;
@property(atomic, retain) UITextField *userName;
@property(retain) UITextField *...
361
votes
7
answers
170k
views
What is the difference between atomic / volatile / synchronized?
How do atomic / volatile / synchronized work internally?
What is the difference between the following code blocks?
Code 1
private int counter;
public int getNextUniqueIndex() {
return counter++...
296
votes
13
answers
293k
views
Practical uses for AtomicInteger
I sort of understand that AtomicInteger and other Atomic variables allow concurrent accesses. In what cases is this class typically used though?
177
votes
5
answers
80k
views
When to use volatile with multi threading?
If there are two threads accessing a global variable then many tutorials say make the variable volatile to prevent the compiler caching the variable in a register and it thus not getting updated ...
145
votes
6
answers
136k
views
When do I really need to use atomic<bool> instead of bool? [duplicate]
Isn't atomic<bool> redundant because bool is atomic by nature? I don't think it's possible to have a partially modified bool value. When do I really need to use atomic<bool> instead of ...
140
votes
4
answers
47k
views
Is file append atomic in UNIX?
In general, what can we take for granted when we append to a file in UNIX from multiple processes? Is it possible to lose data (one process overwriting the other's changes)? Is it possible for data ...
134
votes
6
answers
36k
views
AtomicInteger lazySet vs. set
What is the difference between the lazySet and set methods of AtomicInteger? The documentation doesn't have much to say about lazySet:
Eventually sets to the given value.
It seems that the stored ...
129
votes
4
answers
72k
views
What do each memory_order mean?
I read a chapter and I didn't like it much. I'm still unclear what the differences is between each memory order. This is my current speculation which I understood after reading the much more simple ...
112
votes
4
answers
41k
views
What is the cost of atomic operations?
What is the cost of the atomic operation (any of compare-and-swap or atomic add/decrement)? How much cycles does it consume? Will it pause other processors on SMP or NUMA, or will it block memory ...
104
votes
7
answers
85k
views
Are PostgreSQL functions transactional?
Is a PostgreSQL function such as the following automatically transactional?
CREATE OR REPLACE FUNCTION refresh_materialized_view(name)
RETURNS integer AS
$BODY$
DECLARE
_table_name ALIAS FOR $...
94
votes
5
answers
21k
views
c++, std::atomic, what is std::memory_order and how to use them?
Can anyone explain what is std::memory_order in plain English, and how to use them with std::atomic<>?
I found the reference and few examples here, but don't understand at all.
http://en....
89
votes
3
answers
14k
views
Where is the lock for a std::atomic?
If a data structure has multiple elements in it, the atomic version of it cannot (always) be lock-free.
I was told that this is true for larger types because the CPU can not atomically change the data ...
85
votes
10
answers
56k
views
Django: How can I protect against concurrent modification of database 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/...
79
votes
2
answers
23k
views
difference between standard's atomic bool and atomic flag
I wasn't aware of the std::atomic variables but was aware about the std::mutex (weird right!) provided by the standard; however one thing caught my eye: there are two seemingly-same (to me) atomic ...
75
votes
6
answers
103k
views
How to implement multithread safe singleton in C++11 without using <mutex>
Now that C++11 has multithreading I was wondering what is the correct way to implement lazy initialized singleton without using mutexes(for perf reasons).
I came up with this, but tbh Im not really ...
73
votes
2
answers
3k
views
Is id = 1 - id atomic?
From page 291 of OCP Java SE 6 Programmer Practice Exams, question 25:
public class Stone implements Runnable {
static int id = 1;
public void run() {
id = 1 - id;
if (id == ...
70
votes
4
answers
26k
views
How are atomic operations implemented at a hardware level?
I get that at the assembly language level instruction set architectures provide compare and swap and similar operations. However, I don't understand how the chip is able to provide these guarantees.
...
68
votes
3
answers
40k
views
What operations are atomic in C#?
Is there a systematic way to know whether an operation in C# will be atomic or not? Or are there any general guidelines or rules of thumb?
66
votes
2
answers
28k
views
Django nested transactions - “with transaction.atomic()”
I would like to know if I have something like this:
def functionA():
with transaction.atomic():
#save something
functionB()
def functionB():
with transaction.atomic():
...
64
votes
1
answer
17k
views
Which std::sync::atomic::Ordering to use?
All the methods of std::sync::atomic::AtomicBool take a memory ordering (Relaxed, Release, Acquire, AcqRel, and SeqCst), which I have not used before. Under what circumstances should these values be ...
62
votes
0
answers
2k
views
Per-element atomicity of vector load/store and gather/scatter?
Consider an array like atomic<int32_t> shared_array[]. What if you want to SIMD vectorize for(...) sum += shared_array[i].load(memory_order_relaxed)?. Or to search an array for the first non-...
61
votes
5
answers
11k
views
Are C/C++ fundamental types atomic?
Are C/C++ fundamental types, like int, double, etc., atomic, e.g. threadsafe?
Are they free from data races; that is, if one thread writes to an object of such a type while another thread reads from ...
58
votes
5
answers
28k
views
When is it preferable to use volatile boolean in Java rather than AtomicBoolean? [duplicate]
I've looked at the other volatile vs. Atomicxxxx questions in SO (including this one) and have read the description of java.util.current.atomic, and I am not quite satisfied with the nuances.
If I'm ...
57
votes
5
answers
39k
views
Is rename() atomic?
I am not being able to check this via experiments and could not gather it from the man pages as well.
Say I have two processes, one moving(rename) file1 from directory1 to directory2. Say the other ...
55
votes
2
answers
16k
views
Acquire/Release versus Sequentially Consistent memory order
For any std::atomic<T> where T is a primitive type:
If I use std::memory_order_acq_rel for fetch_xxx operations, and std::memory_order_acquire for load operation and std::memory_order_release ...