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 ...
163
votes
Accepted
Why are concurrent writes not allowed on an SQLite database?
Because "multiple concurrent writes" is much, much harder to accomplish in the core database engine than single-writer, multiple-reader. It's beyond SQLite's design parameters, and including it would ...
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 ...
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 ...
41
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 ...
30
votes
Why coroutines are back?
Coroutines never left, they were just overshadowed by other things in the meanwhile. The recently increased interest in asynchronous programming and therefore coroutines is largely due to three ...
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 ...
28
votes
Accepted
how many cores should I utilize for calculations? #cores or #cores -1?
Major operating systems are mature enough to know how to handle processes which use every available core. Other processes may (and often will) be affected, but the computation won't become slower ...
26
votes
Accepted
ES / CQRS concurrency handling
I sketched my rough understanding on how an ES / CQRS app should look like contextualized to a simplified banking use case (withdrawing money).
This is the perfect example of an event sourced ...
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 ...
18
votes
Accepted
Java - Processing a large file concurrently
The most likely efficient way to do this is:
Have a single thread that reads the input file. Harddisks are at their fastest when reading sequentially.
Do not read it into memory all at once! That is ...
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 ...
15
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 ...
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 ...
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 ...
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 ...
13
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, ...
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
Accepted
Am I looking at multithreading the wrong way? (Java)
A concurrent system is inherently more complex than a single-threaded system. In fact, complexity scales exponentially with the number of threads: If I have three threads that can be in any of 5 ...
12
votes
How to architect a store to avoid overselling inventory (distributed database scenario)
It depends on the widget.
If the widget is rare and expensive (exactly 10 Ferraris), then the approach you're following is correct. Of course, you also need to account for inventory that's being ...
12
votes
Why are concurrent writes not allowed on an SQLite database?
Because there's no server which can tell whether things are to be written to the same place or not. There are just two processes trying to write to a file.
As pointed in a comment, concurrent writes ...
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
how many cores should I utilize for calculations? #cores or #cores -1?
It depends.
If the machine is dedicated to this computation, you should use all cores – unused computing resources don't speed things up.
If you are using a realtime scheduler, a non-preemptive ...
11
votes
In what ways is Rust a "concurrent" language?
It might mean two things :
Capability of concurrent programming
Rust provides standard libraries for creation and running of multiple concurrent executions. Mostly through threading. But this is ...
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 &...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
concurrency × 371multithreading × 101
java × 60
design × 26
locks × 26
parallelism × 23
synchronization × 22
c# × 20
database × 18
c++ × 16
architecture × 15
go × 14
design-patterns × 13
python × 13
algorithms × 11
operating-systems × 11
distributed-system × 10
actor-model × 10
performance × 9
parallel-programming × 9
message-queue × 8
asynchronous-programming × 8
semaphore × 8
programming-languages × 7
data-structures × 7