Skip to main content
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
15 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
15 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
  • 5,824
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

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,197
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
  • 3,378
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
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

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,986
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.4k
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
  • 59.9k
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,986
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
  • 27.8k
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.4k
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

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

Name of locking approach

It sounds like you're talking about Semaphore.
Lie Ryan's user avatar
  • 12.4k
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
  • 27.8k
4 votes

optimistic locking vs pessimistic locking

If collisions are so rare that optimistic locking is a serious option, even if your collision resolution is a bit clumsy, how would that play a role if it happens once a year or less? However, if you ...
Mecki's user avatar
  • 2,350
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
  • 135k
3 votes

Message Queue with multiple consumers locking synchronous on a field

What do you do if the consumer that was handling an order crashed? You do not want to have a consumer receive items on an order and process them. You want an intermediary that builds partial orders ...
Theraot's user avatar
  • 9,171
3 votes

Is the Actor Model an alternative to transactions?

Actor model doesn't magically solve all the troubles. It just makes such an abstraction of concurrent programming that helps reasoning about solutions. There are two major aspects of Actor Model, ...
iTollu's user avatar
  • 315
3 votes

Best-practice to manage concurrency into a basket in a e-commerce website

In our case, we used message queue to handle orders and configured it such that it only processes one job at a time in sequence on the same product, in a FIFO fashion. The caveat is that you're ...
Korayem's user avatar
  • 139
3 votes

optimistic locking vs pessimistic locking

Both locking strategies have their costs. Optimistic locking is more expensive when considering the functional requirements (endless loop danger, showing data history, consolidation logic) ...
Emond's user avatar
  • 1,258
3 votes

How to replace a file to its latest version in server that is being constantly fetched by a REST API

The problem with using a fixed file (i.e. the same file in the same path) is the source of your issue: the inability to update it without making it temporarily unavailable. A better way to do this is ...
Flater's user avatar
  • 52.8k
2 votes

How can I diagram data-locking scenarios?

Petri net formalism combines both graphical representation and mathematical definition. This includes for instance ability to prove deadlock existence. Quoting Wikipedia: A Petri net consists of ...
mouviciel's user avatar
  • 15.5k

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