Hot answers tagged

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, ...
user avatar
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 ...
user avatar
  • 2,585
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 ...
user avatar
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 ...
user avatar
  • 21.1k
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)...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 185k
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 ...
user avatar
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. ...
user avatar
  • 10.2k
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). ...
user avatar
  • 5,632
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 ...
user avatar
  • 9,017
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 ...
user avatar
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 ...
user avatar
  • 45.2k
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 ...
user avatar
  • 15.9k
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 ...
user avatar
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 ...
user avatar
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 ...
user avatar
  • 1,936
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 ...
user avatar
  • 12k
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 ...
user avatar
  • 54.7k
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 ...
user avatar
  • 161
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 ...
user avatar
  • 15.2k
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 ...
user avatar
  • 588
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 ...
user avatar
  • 185k
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, ...
user avatar
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 ...
user avatar
  • 29.8k
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 ...
user avatar
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 ...
user avatar
  • 1,936
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 ...
user avatar
  • 495

Only top scored, non community-wiki answers of a minimum length are eligible