Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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 ...
Philip Kendall's user avatar
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 ...
Jonathan Eunice's user avatar
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 ...
Theraot's user avatar
  • 9,151
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 ...
Iron Gremlin's user avatar
  • 1,115
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 ...
Hans-Martin Mosner's user avatar
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 ...
Jörg W Mittag's user avatar
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 ...
Ilmari Karonen's user avatar
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 ...
amon's user avatar
  • 134k
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 ...
Caleth's user avatar
  • 11.3k
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 ...
Arseni Mourzenko's user avatar
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 ...
Constantin Galbenu's user avatar
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 ...
Lie Ryan's user avatar
  • 12.4k
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 ...
cjs's user avatar
  • 787
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 ...
Michael Borgwardt's user avatar
16 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 ...
Levi Morrison's user avatar
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 ...
Kilian Foth's user avatar
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 ...
Karl Bielefeldt's user avatar
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 ...
Erik Eidt's user avatar
  • 34.1k
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 ...
Theraot's user avatar
  • 9,151
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 ...
Kain0_0's user avatar
  • 16.2k
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, ...
JacquesB's user avatar
  • 59.7k
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 ...
amon's user avatar
  • 134k
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 ...
amon's user avatar
  • 134k
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 ...
Dan Pichelman's user avatar
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 ...
Stop harming Monica's user avatar
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 ...
Chris Jefferson's user avatar
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 ...
amon's user avatar
  • 134k
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 ...
Euphoric's user avatar
  • 37.5k
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 &...
Philippe Verdy's user avatar

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