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, ...
Jörg W Mittag's user avatar
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 ...
Kasper van den Berg's user avatar
14 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 ...
Bart van Ingen Schenau's user avatar
14 votes
Accepted

C#: Refactoring an oversized try/catch/finally

You could consider introducing a IDisposable-object to own whatever resource that needs cleanup, with a using statement or declaration to ensure cleanup. If each resource is in its own disposable ...
JonasH's user avatar
  • 4,357
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 ...
Arseni Mourzenko's user avatar
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). ...
8bittree's user avatar
  • 5,656
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. ...
Random42's user avatar
  • 10.4k
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 ...
Samuel's user avatar
  • 9,167
10 votes

C#: Refactoring an oversized try/catch/finally

I haven't used C# so this is general advice... I would start with some simple method extractions, so your code looks something like this: ctxBefore = doStuffBeforeLock() if (ctxBefore.isAbort()) ...
DavidT's user avatar
  • 2,193
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 ...
Jimmy Hoffa's user avatar
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 ...
Robert Harvey's user avatar
8 votes

Maintaining locks across abstraction

You're asking multiple questions. For the general case of calling multiple decoupled functions with correct mutual exclusion, the solution is to Expose the mutex associated with each function Split ...
Useless's user avatar
  • 12.6k
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 ...
Servy's user avatar
  • 1,966
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 ...
Kasey Speakman's user avatar
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 ...
Useless's user avatar
  • 12.6k
7 votes

Why do we need read locks in read-write locks?

An update lock is an exclusive lock, that is, only one lock can be held at a time. It is used to prevent concurrent updates. A read lock is a shared lock, multiple readers can held a read lock ...
Lie Ryan's user avatar
  • 12.3k
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 ...
JacquesB's user avatar
  • 57.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 ...
Dom's user avatar
  • 161
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 ...
Servy's user avatar
  • 1,966
5 votes
Accepted

Achieving very large real-time async writes on MySQL tables without locking them to reads

Before guessing that it will be slow you actually have to demonstrate that it's slow. This is in the fundamentals of performance optimization Hundreds of thousands of users, times game count for each ...
Silviu-Marian's user avatar
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 ...
lutzh's user avatar
  • 505
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 ...
Robert Harvey's user avatar
5 votes
Accepted

Why would CPython logging use a Lock for each handler rather than one Lock per Logger?

Logging handlers can be slow, especially if you do remote logging to another machine, using for example SocketHandler/SMTPHandler/HTTPHandler, or to third party service logging handlers. If the entire ...
Lie Ryan's user avatar
  • 12.3k
5 votes

What is the typical best practice methodology around transferring money between two distinct systems?

So the first thing that you have to understand is that real world systems that accounts do not have a single total that is added-to and subtracted from. Instead what they have is a set of transaction ...
JimmyJames's user avatar
  • 25.9k
5 votes

Why do we need read locks in read-write locks?

Presuming a write cannot be done atomically: Only one thread at a time can safely write data without corruption. No reads can happen during a write without getting corrupt data. Multiple reads can ...
Craig Tullis's user avatar
  • 1,975
4 votes

How is spinlock different from polling?

The difference is that a spinlock is (hopefully) only used in situations where it is appropriate, and in these situations it is very efficient. You use a spinlock if you expect that a resource will ...
gnasher729's user avatar
  • 43.4k
4 votes

Name of locking approach

It sounds like you're talking about Semaphore.
Lie Ryan's user avatar
  • 12.3k
4 votes

Is there a problem using A LOT of locks in a process?

In short: Yes. Or rather, no, but your question hints to a fundamental conceptual misdesign, so... yes. It's actually "no" because lightweight locks which take very few resources, and few ...
Damon's user avatar
  • 253
4 votes
Accepted

Race condition in Distributed Systems

This kind of issue is common in distributed systems. The simplest solution is to combine the check for whether a change is allowed with the request to make the change. This technique is useful in a ...
JimmyJames's user avatar
  • 25.9k
4 votes
Accepted

My aproach to lockless concurrency

Yes, that's not entirely unreasonable. In practice, you would likely replace the (atomic) counter by a semaphore but all of that is more or less equivalent. A synchronization method that ensures no ...
amon's user avatar
  • 133k

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