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 ...
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 ...
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 ...
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
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
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())
...
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 ...
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
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
4
votes
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 ...
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 ...
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 ...
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 ...
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)
...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
locks × 80concurrency × 26
multithreading × 22
c# × 12
java × 8
synchronization × 7
transaction × 7
design × 5
message-queue × 3
distributed-system × 3
asynchronous-programming × 3
design-patterns × 2
architecture × 2
c++ × 2
algorithms × 2
database × 2
.net × 2
programming-languages × 2
database-design × 2
microservices × 2
sql × 2
terminology × 2
relational-database × 2
scalability × 2
parallelism × 2