181
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 ...
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 ...
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 ...
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 ...
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.
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 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 ...
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 ...
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 ...
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
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 ...
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 ...
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 ...
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 ...
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 ...
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 - ...
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 ...
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 ...
15
votes
Can multi-threading improve performance of an IO-bound process?
I/O performance has two aspects that you must distinguish: latency and throughput. One measures how long you have to wait for the first byte of a response, the other how long you have to wait ...
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 ...
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
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 ...
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 ...
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 ...
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 ...
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
Accepted
Why use ExecutorService for long-running thread?
I think that "Never" is too strong a word. It's more like the second rule of optimization: "Don't (yet)".
In my opinion, the primary reason to use an executor service is to manage the number of ...
12
votes
Accepted
Immediately awaiting an asynchronous call
I think you misunderstand the await call. What async and await do is allow the thread you are executing on (not just the main thread) to continue working. The mechanism that supports this is called ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
multithreading × 685java × 123
concurrency × 101
c# × 87
c++ × 76
design × 52
c × 40
design-patterns × 30
architecture × 29
.net × 29
performance × 25
python × 24
asynchronous-programming × 22
locks × 22
synchronization × 20
parallelism × 20
operating-systems × 19
cpu × 19
multiprocessing × 16
c++11 × 15
async × 15
parallel-programming × 14
multi-core × 14
linux × 13
io × 13