86
votes
Accepted
How is spinlock different from polling?
Polling refers to repeatedly checking whether a resource (any kind of resource) is ready.
A spinlock is when the resource you are polling is a lock.
Note that polling is not bad. In particular, ...
37
votes
Accepted
What prevents a race condition on a lock?
Is it impossible, or just plain unlikely?
Impossible. It can be implemented in different ways, e.g., via the Compare-and-swap where the hardware guarantees sequential execution. It can get a bit ...
35
votes
Accepted
Is there a problem using A LOT of locks in a process?
Using many locks may cause some problems:
The number locks a process may request is often limited by the OS.
Your system may, indeed, run out of locks.
Note: this depends on the kind of lock. The ...
17
votes
What prevents a race condition on a lock?
Study the concept of atomic "Test and Set" operations.
Essentially the operation cannot be divided - it is not possible for two things to do it at exactly the same time. It will check a value, set ...
14
votes
Accepted
Reading file during write on linux
Your understanding is wrong. Several Linux processes can write to the same file at once (and what happens then might be unspecified, except when the processes overwrite different segments of that file)...
12
votes
What is the actual reason that locks (sentinels) in OO are hard to reason about?
It's not the locking that is hard to understand. Locks are easy. They work just like the locks in real life. It's the concurrency that's hard to understand.
The only reason locking makes people's ...
11
votes
How much work should I place inside a lock statement?
IMHO you are asking the wrong question - you should not care so much about efficiency trade-offs, but more about correctness.
The first variant makes sure _runningWorkers is only accessed during a ...
11
votes
How to replace a file to its latest version in server that is being constantly fetched by a REST API
Have the API point to a different file.
When an HTTP GET request arrives, the API won't just load (and send) a file from a hardcoded file path. Instead, if will:
Check the directory for the available ...
10
votes
Why is Akka good for concurrency?
I'm going to write about the Actor Model in general (not just Akka) in comparison with other concurrency models such as the classical lock-based concurrency and the neat transactional memory.
...
10
votes
How is spinlock different from polling?
A spinlock is a type of lock, specifically, one achieved via polling.
Polling is a method of checking the status of something (by asking for the status, as opposed to waiting to be told the status).
...
10
votes
Is there a problem using A LOT of locks in a process?
As mentioned in the comments, as you scale you'll run into problems trying to keep state in memory. What happens if your server crashes? Do all your users lose their notifications? As you scale up you ...
10
votes
Why do we need read locks in read-write locks?
A read lock allows multiple concurrent readers of some data, but it prevents readers from accessing the data while a writer is in the middle of changing it. That ensures that a reader will never see a ...
9
votes
How much work should I place inside a lock statement?
The other answers are quite good and clearly address the correctness concerns. Let me address your more general question:
How much work should I place inside a lock statement?
Let's start with the ...
9
votes
Accepted
What is it called when threads are waiting but not deadlocked
The term for this type of behaviour is Thread Synchronization. Also sometimes called serialization because the threads must get in line and go through that piece of code serially.
Lock Contention is ...
8
votes
Composable concurrency in Java or any other programming language
It's not the Java language. It's the nature of locks (mutexes).
There are better ways to get improved concurrency while still guaranteeing correctness, ways that are language independent:
Using ...
7
votes
Accepted
Why is Akka good for concurrency?
One of the advantages of message-processing models like actors and agents is that the traditional concurrency problems (primarily synchronization of shared state) are no longer a problem. The actor ...
7
votes
When is it safe to use Monitor (lock) with Task?
So I want to ask how I can be sure that Monitor is safe with a Task (or: how can I be sure that a Task is running on a Thread of its own).
You don't. You shouldn't be using a Monitor in an ...
7
votes
Accepted
How to check whether existing code base can deadlock
I've already adhered to best practices, like never have more than one lock locked at a time, always lock multiple locks in the same order, etc
Well, if you only hold one lock at a time, the second ...
7
votes
What will you do if multiple users access your application at the same time?
This is a really broad question, since concurrency issues are handled in a variety of ways in complex system. For example a synchronized block in Java is only locked for other threads in the same ...
6
votes
PHP file_put_contents File Locking
I know this is ages old, but in case someone runs into this. IMHO the way to go about it is like this:
1) Open the original file (e.g. original.txt) using file_get_contents('original.txt').
2) Make ...
6
votes
What is the actual reason that locks (sentinels) in OO are hard to reason about?
I don't have any experience with STM but I'll address the locking part.
Getting single threaded code right is hard. It's pretty telling that the prevalent methodology is to create a large battery of ...
5
votes
Why don't we see (more) widespread adoption of lock-free dynamic memory allocators?
Here are some possible answers:
IBM has patented their lock-free allocator... But then they also support Linux etc., so they might have an incentive to at least provide an implementation. And the ...
5
votes
Accepted
Alternative locking strategies
I would try to solve this with (at least) four document states
unprocessed
export started (but not under processing)
under processing (means: after export finished)
processed
Moreover, it will be ...
5
votes
Accepted
Scaling locks in high concurrency web apps
Welcome to the deep, dark recesses of the Concurrency Jungle! This is where many app developers fear to tread--and fear it for good reason.
@kdgregory gets it exactly right: This is not one problem, ...
5
votes
What is the actual reason that locks (sentinels) in OO are hard to reason about?
The particular issue with OOP and concurrency is that often programmers are tempted to try to hide thread safety implementation away using encapsulation.
When this is done by baking locks right into ...
5
votes
How is spinlock different from polling?
Spinlock is different from polling because it only occurs for a very short period of time, on the order of a few milliseconds or less. Polling can go on indefinitely.
In parallel programming, a ...
5
votes
Accepted
Race condition allowing multiple transactions; will locking on objects in a dictionary cause issues?
It's not safe for you to be searching through the dictionary outside of a lock, because by doing so another dictionary could be writing to it at the same time, so your read could end up being ...
5
votes
Is the Actor Model an alternative to transactions?
You state correctly that transactions and locking are needed to deal with shared mutable state. And also that in an actor system, there is no shared mutable state. So it seems you've already answered ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
locks × 76concurrency × 26
multithreading × 21
c# × 11
java × 8
synchronization × 7
transaction × 7
design × 5
message-queue × 3
asynchronous-programming × 3
distributed-system × 3
design-patterns × 2
architecture × 2
algorithms × 2
database × 2
.net × 2
programming-languages × 2
database-design × 2
microservices × 2
terminology × 2
sql × 2
relational-database × 2
scalability × 2
parallelism × 2
actor-model × 2