182
votes
Does it ever make sense to use more concurrent processes than processor cores?
The canonical time when you use far, far more processes than cores is when your processes aren't CPU bound. If your processes are I/O bound (either disk or more likely network), then you can ...
152
votes
Accepted
How do I mitigate a scenario where a user goes to pay, but the price is changed mid-request?
Alice wants to pay Bob for a service. Bob has quoted her $10.
Give this quote a unique token.
Alice clicks pay.
When this response is send to the server, it must go with the token of what ...
59
votes
Does it ever make sense to use more concurrent processes than processor cores?
Short answer: Yes.
Longer answer:
Set your magic number stupid high, benchmark it, set it low, benchmark it again, and keep doing that until you have your answer.
The number of moving parts here is ...
58
votes
How do I mitigate a scenario where a user goes to pay, but the price is changed mid-request?
A quote should be a write-once record.
Bob isn't allowed to edit it once it has been created and passed to Alice.
You can ensure this at different levels, from simply not offering an edit dialog to ...
54
votes
Do compilers optimise in concurrency?
Asuming expensive_calc_one and expensive_calc_two are pure functions
Unfortunately, determining whether a function is pure is equivalent to solving the Halting Problem in the general case. So, you ...
42
votes
Is "releases mutexes in reverse order" required to make this deadlock-prevention method work?
For a deadlock (more specifically, a circular wait) to occur, there needs to be a circular chain of n ≥ 2 mutexes (or other exclusively lockable resources) R1, R2, …, Rn such that, for each k from 1 ...
29
votes
How do I mitigate a scenario where a user goes to pay, but the price is changed mid-request?
This must be an extremely common problem to deal with, no?
No, it isn't. I doubt you'll be able to find a payment processor that lets you change the amount after the customer has authorised a ...
21
votes
Accepted
What should I do when optimistic locking doesn't work?
The ETag mechanism specifies only the communication protocol for optimistic locking. It's the responsibility of the application service to implement the mechanism to detect concurrent updates to ...
20
votes
How do I mitigate a scenario where a user goes to pay, but the price is changed mid-request?
Just send the amount Alice agreed to pay along with the request. If the price has increased since Alice sent the request, you send a response indicating that the item could not be purchased at or ...
17
votes
The difference between "concurrent" and "parallel" execution?
I believe this answer to be more correct than the existing answers and editing them would have changed their essence. I have tried to link to various sources or wikipedia pages so others can affirm ...
16
votes
Accepted
Alternative to Actor model
Three paradigms in common use are actors (e.g. Akka), Software Transactional Memory (e.g. Clojure) or traditional manual lock-wrangling. They all have their own challenges.
Traditional lock-based ...
14
votes
Accepted
How do you test and demonstrate that you have properly prevented a race condition?
Sometimes you have some control over the timing, and you can intentionally force a race in a test. When you are using a guarantee of an external system like a database, you usually can't control the ...
14
votes
Accepted
Can functional programming languages have deadlock conditions?
The quote is correct in principle. If you don't have any mutable state or side effects then you don't need to lock anything and without locks you don't get deadlocks.
But in reality, most programs, ...
13
votes
What should I do when optimistic locking doesn't work?
You have to execute the following pair atomically:
checking of the tag for validity (i.e. is up to date)
updating the resource (which includes updating its tag)
Others are calling this a transaction &...
13
votes
Accepted
Implementing a hash table with true concurrency
I have wrestled with this. I did a few iterations on my design...
First solution: Just have a hash table with a global lock.
Second solution: Wait free fixed size hash table. We need to figure out ...
13
votes
Is "releases mutexes in reverse order" required to make this deadlock-prevention method work?
For a deadlock to occur a system has to have several properties simultaneously. Wikipedia has some more details on this but for short:
Mutual Exclusion
Incremental Acquisition
No preemption
Circular ...
12
votes
Accepted
Is it possible to achieve Rust's ownership model with a generic C++ wrapper?
C++ has three ways to pass parameters to a function: by value, by lvalue reference, and by rvalue reference. Of these, passing by value creates ownership in the sense that the called function receives ...
12
votes
Does it ever make sense to use more concurrent processes than processor cores?
In A.I. it is common for people to observe super-linear speedups when they write parallel algorithms (that is, > K times speedup with K processes running on K cores). This is because you are often ...
11
votes
Does it ever make sense to use more concurrent processes than processor cores?
You can take the example of compiled Linux distributions (like Gentoo): to optimize the compilation time, it is obviously using parallel compilation using more processes than the number of available &...
11
votes
Is "releases mutexes in reverse order" required to make this deadlock-prevention method work?
Let's have a look at the simplest form of deadlock: the kiss of death of two processes trying to acquire 2 mutexes:
(1) | (2)
Lock mutex A (success) | Lock mutex ...
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 ...
11
votes
Accepted
How can a web browser simultaneously run more videos than the number of CPU cores?
is each window running a separate thread, where the threads are switching quicker than I can differentiate them?
Yep, pretty much this. If you look at task manager or other tool you will see ...
9
votes
Accepted
Why datatypes are marked as thread-safe instead of procedures?
There is nothing intrinsic to a function that implies thread safety other than its awareness of what shared data may be modified, and how it protects that data. It is the data itself that exists in ...
9
votes
Do critical section and atomicity imply each other?
Atomicity should be understood to mean that the thing it refers to (a transaction, an operation, etc.) is all-or-nothing. There is no defined mechanism to observe partial results.
For a database ...
9
votes
Do compilers optimise in concurrency?
Compilers are not generally smart enough to do this, in particular because most languages don't have a sufficiently reliable concept of a “pure function”. There can be rather subtle interactions ...
9
votes
How do you test and demonstrate that you have properly prevented a race condition?
One possibility is to replace the scheduler with one that is under your control.
That is essentially how Microsoft Research's CHESS tool works. It uses static analysis to try and determine all ...
8
votes
How does a movie theater seat booking system prevent multiple users from reserving the same seats?
The 30 seconds you have seen are nowadays often more like 15 mins. I don't believe there is a database transaction active for that duration.
If I was to design such a system, this is how I would do ...
8
votes
How Functional Programming addresses concurrent increment/decrement operations invoked by different users?
You are asking how functional languages handles shared mutable state. Functional languages do prefer and encourage immutable data structures, but they all (AFAIK) have provisions for mutable shared ...
8
votes
How do I mitigate a scenario where a user goes to pay, but the price is changed mid-request?
Yes this is a common issue, and it is about transactional consistency.
To summarize your issues:
the quote is binding for the seller. In general it has a reference and an expiration date/time.
the ...
8
votes
SQL databases - what will happen if dozen of users edit the same record at the same time?
A "user who edits a record" assumes there is not only a database involved in the process, but also an application where a user can edit data in some user interface.
And here it boils down to ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
concurrency × 379multithreading × 101
java × 60
design × 26
locks × 26
parallelism × 23
c# × 22
synchronization × 22
database × 19
c++ × 16
architecture × 15
go × 15
design-patterns × 13
python × 13
algorithms × 11
operating-systems × 11
distributed-system × 10
actor-model × 10
performance × 9
message-queue × 9
asynchronous-programming × 9
parallel-programming × 9
transaction × 8
semaphore × 8
programming-languages × 7