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
95 votes
Accepted

How does a single thread run on multiple cores?

The operating system offers time slices of CPU to threads that are eligible to run. If there is only one core, then the operating system schedules the most eligible thread to run on that core for a ...
Erik Eidt's user avatar
  • 34.2k
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
51 votes
Accepted

What causes unpredictability when doing multi threading

Your understanding of computer hardware is flawed. Memory is not accessed by different cores in parallel, access is regulated like traffic at a road junction. Different threads can run simultaneously ...
Martin Maat's user avatar
  • 18.5k
41 votes

Why is multithreading not used everywhere?

Why multithreading isn't everywhere? Because … I understand that multi-threading is hard to implement and has drawbacks if number of threads is less than expected.
Jörg W Mittag'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
  • 135k
29 votes

How does a single thread run on multiple cores?

There is no such thing as a single thread running on multiple cores simultaneously. It doesn't mean, however, that instructions from one thread cannot be executed in parallel. There are mechanisms ...
Frax's user avatar
  • 1,844
29 votes

How does a single thread run on multiple cores?

summary: Finding and exploiting the (instruction-level) parallelism in a single-threaded program is done purely in hardware, by the CPU core it's running on. And only over a window of a couple ...
Peter Cordes's user avatar
29 votes
Accepted

Why is multithreading not used everywhere?

The proliferation of multi-core CPUs is predominantly driven by supply, not by demand. You're right that many programmers don't bother decomposing their systems so that they can profit from multiple ...
Kilian Foth's user avatar
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

Why do modern operating systems *ever* have perceptible input (keyboard/mouse) lag?

As you may have noticed, there's a category of application that tries really hard to avoid input lag and only occasionally fails at doing so: games. Even then it's not uncommon for players to notice ...
pjc50's user avatar
  • 13.7k
25 votes

Why do modern operating systems *ever* have perceptible input (keyboard/mouse) lag?

I would like to answer this question from more of a high-level, marketing perspective than a more low-level, technical one. All of the current mainstream Operating Systems are so-called general ...
Jörg W Mittag's user avatar
24 votes
Accepted

Writing public libraries: Should I let the consumer of the library enforce thread safety?

You should make your library thread-safe, but that does not mean that you should be sprinkling synchronization primitives around your code base. For the average library, which is not explicitly ...
Bart van Ingen Schenau's user avatar
23 votes

Why is multithreading not used everywhere?

Why multithreading isn't everywhere? Frame challenge: but it is everywhere. Let's see, let's name some platforms: Desktops/laptops: one of the most common applications today is the browser. And to ...
Vilx-'s user avatar
  • 5,360
20 votes
Accepted

How does sleeping a thread work?

There is much more involved in running a program than just the code within that program. Any program that runs in a multi-process OS is under the control of the OS's scheduler, and the scheduler does ...
Kilian Foth's user avatar
20 votes

How to prevent two users from registering at the same instant with same username?

Let's say the database doesn't contain username 'user1'. When two users try to register at the same moment with 'user1' it'll accept it. Why would it accept it? It's simple to apply a unique ...
DeadMG's user avatar
  • 36.9k
20 votes

Why do modern operating systems *ever* have perceptible input (keyboard/mouse) lag?

Why can't (or why don't) operating systems absolutely prioritise user input (and repainting thereof) in threading and process scheduling? Even if the operating system tells the application about the ...
Caleth's user avatar
  • 11.5k
18 votes

How does sleeping a thread work?

As Doc Brown mentioned in a comment, interrupts are the key, and not just for sleeping. An interrupt is a hardware signal that the processor should stop what it's doing and run a piece of code. ...
kdgregory's user avatar
  • 5,250
18 votes

Why is there a shift towards asynchronous and event driven programming?

The "async" approach better facilitates human reasoning. When most people drive, they don't need to concern themselves with how every element of the car is interacting. Forget the tires - ...
TheRubberDuck'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
16 votes
Accepted

How could thread safety be provided by a programming language similar to the way memory safety is provided by Java and C#?

Races occur when you have simultaneous aliasing of an object and, at least, one of the aliases is mutating. So, to prevent races, you need to make one or more of these conditions untrue. Various ...
Alex's user avatar
  • 3,912
14 votes
Accepted

Unit test which asserts that current thread is the main thread

Let me show you my favorite unit test principles: A test is not a unit test if: It talks to the database It communicates across the network It touches the file system It can't run at ...
candied_orange'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
Accepted

Is it okay to start a thread from within a constructor of a class

Initializing the object completely in the constructor is usually preferable because it reduces the number of states, and thus making your object easier to reason about. No start and stop methods ...
amon's user avatar
  • 135k
13 votes

How could thread safety be provided by a programming language similar to the way memory safety is provided by Java and C#?

Java and C# provide memory safety by checking array bounds and pointer dereferences. It's important to first think about how C# and Java do this. They do so by converting what is undefined behaviour ...
Eric Lippert's user avatar
  • 46.3k
13 votes
Accepted

If the whole point of recursion is to break the problem into multiple smaller problems, what if those problems were solved in parallel?

Recursion isn't really about breaking problems into smaller ones or enabling concurrency; it's about solving self-similar problems. Consider this representation of a Binary Search Tree: public class ...
Robert Harvey's user avatar
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,171
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.9k

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