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
16 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
  • 6,329
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,247
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
  • 4,629
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.9k
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.9k
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.5k
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
  • 62.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
  • 30.9k
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.5k
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,995
4 votes

Name of locking approach

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

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,390
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
  • 30.9k
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

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,261
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
  • 59.5k
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
2 votes
Accepted

Name of locking approach

I think you talking about ReentrantLock: The ReentrantLock provides synchronization to methods while accessing shared resources. The code which manipulates the shared resource is surrounded ...
NiteenUgale90's user avatar
2 votes

Locking an item while waiting for human approval

Add an enumeration field to your item called "Status," and a DateTime field to store the time that the item entered the approval process. Allow the Status field to have as many values as you need to ...
Robert Harvey's user avatar
2 votes

Race condition in Distributed Systems

I think you said it in your first sentence - the solution is made more complex when you have intertwined or distributed validation logic. My first step would be to refactor the validation logic into a ...
Martin K's user avatar
  • 2,947
2 votes

Will this time duration measurement in two threads give correct result?

First of all, realize that performance measurements never give correct results, just more or less incorrect ones. You're striving for less incorrect, so you need to eliminate the factors that ...
Hans-Martin Mosner's user avatar
2 votes
Accepted

Calling an API and synchronising database

What you're trying to do is essentially a distributed transaction, and it is a very tricky thing to get right, if at all possible. There are a couple of things you could do. First, you could try to ...
Robert Bräutigam's user avatar
2 votes

Building a function call tree at runtime

In light of your edit, I'd say your approach of building a call graph from a custom static analyser tool - and deriving enough useful information about the database locking from a large codebase - is ...
Steve's user avatar
  • 12.6k

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