Skip to main content
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
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 ...
Theraot's user avatar
  • 9,261
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
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
  • 12.4k
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.5k
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
  • 783
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 ...
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
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, ...
JacquesB's user avatar
  • 62.4k
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 &...
Erik Eidt's user avatar
  • 34.8k
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,261
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.6k
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
  • 136k
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

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
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 ...
Christophe's user avatar
  • 82.3k
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
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 ...
LoztInSpace's user avatar
  • 1,348
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 ...
cyberbisson's user avatar
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 ...
Bart van Ingen Schenau's user avatar
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 ...
amon's user avatar
  • 136k
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 ...
Jörg W Mittag's user avatar
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 ...
Hugo G's user avatar
  • 270
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 ...
JacquesB's user avatar
  • 62.4k
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 ...
Christophe's user avatar
  • 82.3k
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 ...
Doc Brown's user avatar
  • 221k

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