Questions tagged [multithreading]

Multi-threading related questions including technique, structure, and safety issues.

Filter by
Sorted by
Tagged with
137 votes
3 answers
62k views

Why Was Python Written with the GIL?

The global interpreter lock (GIL) seems to be often cited as a major reason why threading and the like is a touch tricky in Python - which raises the question "Why was that done in the first place?" ...
Fomite's user avatar
  • 2,626
103 votes
13 answers
74k views

What can multiple threads do that a single thread cannot? [closed]

While threads can speed up execution of code, are they actually needed? Can every piece of code be done using a single thread or is there something that exists that can only be accomplished by using ...
80 votes
4 answers
90k views

What is a thread pool?

How would one implement a threadpool? I've been reading on wikipedia for "threadpool" but I still can't figure out what one should do to solve this question (possibly because I didn't quite understand ...
John Smith's user avatar
  • 1,719
79 votes
1 answer
22k views

Is there a difference between fibers, coroutines and green threads and if that is so what is it?

Today I was reading several articles on the Internet about fibers, coroutines and green threads, and it seems like these concepts have very much in common, but there are slight differences, especially ...
DejanLekic's user avatar
73 votes
3 answers
62k views

How does a single thread run on multiple cores?

I am trying to understand, at a high-level, how single threads run across multiple cores. Below is my best understanding. I do not believe it is correct though. Based on my reading of Hyper-threading,...
Evorlor's user avatar
  • 1,442
69 votes
6 answers
27k views

Testing multi-threaded race conditions

Reading the comments to this answer, specifically: Just because you can't write a test doesn't mean it's not broken. Undefined behaviour which usually happens to work as expected (C and C++ are ...
Dan Is Fiddling By Firelight's user avatar
62 votes
1 answer
29k views

Are go-langs goroutine pools just green threads?

The commentator here offers the following criticism of green threads: I was initially sold on the N:M model as a means of having event driven programming without the callback hell. You can write code ...
hawkeye's user avatar
  • 4,819
61 votes
10 answers
24k views

Does it ever make sense to use more concurrent processes than processor cores?

I've got some process in Go. Here's an example counting lines in text, though the question is meant to be far more general than this particular example: func lineCount(s string) int { count := 0 ...
TheEnvironmentalist's user avatar
60 votes
15 answers
26k views

How important is multithreading in the current software industry? [closed]

I have close to 3 years experience writing web applications in Java using MVC frameworks (like struts). I have never written multithreaded code till now though I have written code for major retail ...
user2434's user avatar
  • 1,277
55 votes
12 answers
10k views

Why would a program require a specific minimum number of CPU cores?

Is it possible to write code (or complete software, rather than a piece of code) that won't work properly when run on a CPU that has less than N number of cores? Without checking it explicitly and ...
uylmz's user avatar
  • 1,129
53 votes
16 answers
7k views

Should I take care of race conditions which almost certainly has no chance of occuring?

Let's consider something like a GUI application where main thread is updating the UI almost instantaneously, and some other thread is polling data over the network or something that is guaranteed to ...
52 votes
8 answers
13k views

What are the drawbacks of making a multi-threaded JavaScript runtime implementation? [closed]

I've been working on a multi-threaded JavaScript runtime implementation for the past week. I have a proof of concept made in C++ using JavaScriptCore and boost. The architecture is simple: when the ...
voodooattack's user avatar
50 votes
19 answers
12k views

Servicing background tasks on a large site

We're dealing with an interesting problem on StackOverflow. We've got a whole bunch of little "needs to be done soon-ish" tasks. An example is updating "Related Questions" lists. What we've done in ...
Kevin Montrose's user avatar
43 votes
10 answers
28k views

Why should your code not use 100% CPU? [closed]

I'm speaking specifically about a C# .NET 4 program running on Windows XP or higher, but general answers are also acceptable. Assume an already optimized and efficient program. The problem here is ...
Nick Udell's user avatar
  • 1,214
40 votes
5 answers
14k views

Does immutability entirely eliminate the need for locks in multi-processor programming?

Part 1 Clearly Immutability minimizes the need for locks in multi-processor programming, but does it eliminate that need, or are there instances where immutability alone is not enough? It seems to me ...
GlenPeterson's user avatar
  • 14.9k
39 votes
3 answers
17k views

Why not Green Threads?

Whilst I know questions on this have been covered already (e.g. https://stackoverflow.com/questions/5713142/green-threads-vs-non-green-threads), I don't feel like I've got a satisfactory answer. The ...
redjamjar's user avatar
  • 819
38 votes
4 answers
5k views

Why is it the caller's responsibility to ensure thread safety in GUI programming?

I have seen, in many places, that it is canonical wisdom1 that it is the responsibility of the caller to ensure you are on the UI thread when updating UI components (specifically, in Java Swing, that ...
durron597's user avatar
  • 7,590
38 votes
3 answers
30k views

Can someone explain in simple terms what is the disruptor pattern?

I would like if you could explain to me in a simple way how does the disruptor patter work. This concept has been elusive to me as of know. Perhaps with your help I could comprehend it.
chrisapotek's user avatar
37 votes
7 answers
4k views

Are there deprecated practices for multithread and multiprocessor programming that I should no longer use?

In the early days of FORTRAN and BASIC, essentially all programs were written with GOTO statements. The result was spaghetti code and the solution was structured programming. Similarly, pointers can ...
DeveloperDon's user avatar
  • 4,958
37 votes
2 answers
16k views

Spurious wakeups explanation sounds like a bug that just isn't worth fixing, is that right?

According to the Wikipedia article on Spurious Wakeups "a thread might be awoken from its waiting state even though no thread signaled the condition variable". While I've know about this 'feature'...
James's user avatar
  • 4,338
36 votes
6 answers
29k views

How would you practice concurrency and multi-threading? [closed]

I've been reading about concurrency, multi-threading, and how "the free lunch is over". But I've not yet had the possibility to use MT in my job. I'm thus looking for suggestions about what I ...
Xavier Nodet's user avatar
  • 3,694
35 votes
8 answers
7k views

When would you need "hundreds of thousands" of threads?

Erlang, Go, and Rust all claim in one way or another that they support concurrent programming with cheap "threads"/coroutines. The Go FAQ states: It is practical to create hundreds of thousands of ...
user39019's user avatar
  • 605
35 votes
8 answers
21k views

How do I unit test multi-threaded code? [duplicate]

Are there ways to unit test your multi-threaded code for race conditions and deadlocks? To see if they are performing the way they should be...
Tamara Wijsman's user avatar
33 votes
7 answers
7k views

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

Sometimes computers stutter a bit when they're working hard, to the point where the mouse location freezes for a fraction of a second, or stutters intermittently for a few seconds. This sometimes ...
Paul Calcraft's user avatar
32 votes
3 answers
61k views

UML Diagrams of Multi-Threaded Applications

For single-threaded applications I like to use class diagrams to get an overview of the architecture of that application. This type of diagram, however, hasn’t been very helpful when trying to ...
PersonalNexus's user avatar
31 votes
3 answers
17k views

Is GCC dying without threads support on Windows? [closed]

I need some opinion. GCC was always a very good compiler, but recently it is losing "appeal". I have just found that on Windows GCC does not have std::thread support, forcing Windows users to use ...
CoffeDeveloper's user avatar
29 votes
2 answers
10k views

How does sleeping a thread work?

When you sleep a thread, what is actually going on? I see that sleeping a thread "pauses the current thread for a given period of time". But just how does it work? According to How Thread.sleep() ...
Rowan Freeman's user avatar
28 votes
10 answers
23k views

State machines vs threads

Alan Cox once said "A Computer is a state machine. Threads are for people who can't program state machines". Since asking Alan directly is not an option for humble me, I'd rather ask here: how does ...
Victor Sorokin's user avatar
27 votes
4 answers
11k views

What prevents a race condition on a lock?

I understand the basics of what data races are, and how locks/mutexes/semaphores help prevent them. But what happens if you have a "race condition" on the lock itself? For example, two different ...
Gavin D. Howard's user avatar
25 votes
11 answers
4k views

Plagued by multithreaded bugs

On my new team that I manage, the majority of our code is platform, TCP socket, and http networking code. All C++. Most of it originated from other developers that have left the team. The current ...
koncurrency's user avatar
25 votes
3 answers
18k views

Why is multithreading often preferred for improving performance?

I have a question, it's about why programmers seems to love concurrency and multi-threaded programs in general. I'm considering 2 main approaches here: an async approach basically based on signals, ...
user1849534's user avatar
25 votes
7 answers
19k views

What do you look for when debugging deadlocks?

Recently I've been working on projects that heavily use threading. I think that I'm OK at designing them; use stateless design as much as possible, lock access to all resources that more than one ...
Michael K's user avatar
  • 15.6k
24 votes
9 answers
56k views

Multithreading synchronization interview question: Find n words given m threads

Is there a way this problem could benefit from a solution with multiple threads, rather than a single thread? In an interview, I was asked to solve a problem using multiple threads. It appears to me ...
rplusg's user avatar
  • 350
23 votes
6 answers
4k views

Multithreading: am I doing it wrong?

I'm working on an application that plays music. During playback, often things need to happen on separate threads because they need to happen simultaneously. For example, the notes of a chord need to ...
Aviv Cohn's user avatar
  • 21.2k
22 votes
4 answers
9k views

Why coroutines are back? [closed]

Most of the groundwork for coroutines occurred in the 60s/70s and then stopped in favor of alternatives(e.g., threads) Is there any substance to the renewed interest in coroutines that has been ...
user1787812's user avatar
22 votes
3 answers
17k views

Do compilers utilize multithreading for faster compile times?

If I remember my compilers course correctly, the typical compiler has the following simplified outline: A lexical analyzer scans (or calls some scanning function on) the source code character-by-...
8protons's user avatar
  • 1,369
21 votes
19 answers
19k views

How would you explain multi threading to a seven year old kid?

If you have to explain the concept of multi-threading to a seven year old kid how would you do it? I recently got this question in an interview. I came up with a story using jobs (the task to be done) ...
20 votes
8 answers
5k views

What causes unpredictability when doing multi threading

Multi threading could cause a racing condition if two threads are accessing the same memory slot, but why is that? From a HW point of view, if the two cores are designed the same, internal pipelines ...
Dan's user avatar
  • 319
20 votes
5 answers
30k views

Can multiple CPU's / cores access the same RAM simultaneously?

This is what I guess would happen: If two cores tried to access the same address in RAM, one would have to wait for the other to access the RAM. The second time that each core would try to access ...
Lost Hobbit's user avatar
19 votes
3 answers
4k views

How do you test and demonstrate that you have properly prevented a race condition? [duplicate]

Suppose I'm building a web application using Django. Some of the views need to touch multiple database tables or rows, and there is some kind of state consistency that I need to ensure among the ...
Kal's user avatar
  • 345
19 votes
2 answers
1k views

Why does shared state degrade performance?

I've been working under the share-nothing principle of concurrent programming. Essentially, all my worker threads have immutable read-only copies of the same state which is never shared between them (...
JoeGeeky's user avatar
  • 811
18 votes
7 answers
18k views

Can you explain why multiple threads need locks on a single-core CPU?

Assume these threads run in single core cpu. As a cpu only run one instruction in one cycle. That is said, even thought they share the cpu resource. but the computer ensure that one time one ...
pythonee's user avatar
  • 339
18 votes
3 answers
21k views

BackgroundWorker vs. Async/Await

I am new to C# development and wish to create a more responsive UI. In my preliminary research, I have seen two methods for achieving this: Multi-threading in conjunction with the BackgroundWorker ...
robert.ecot's user avatar
17 votes
4 answers
7k views

Programs that claim they are not "multi-core" friendly

You see this phrase or similar kicked around from time to time, generally referring to a program that claims they were not designed to take full advantage of multi-core processors. This is common ...
SnakeDoc's user avatar
  • 361
17 votes
3 answers
5k views

Naming Convention for Dedicated Thread Locking objects [closed]

A relatively minor question, but I haven't been able to find official documentation or even blog opinion/discussions on it. Simply put: when I have a private object whose sole purpose is to serve for ...
Chris Sinclair's user avatar
17 votes
2 answers
1k views

How can I make a universal construction more efficient?

A "universal construction" is a wrapper class for a sequential object that enables it to be linearized (a strong consistency condition for concurrent objects). For instance, here's an adapted wait-...
VF1's user avatar
  • 1,891
16 votes
6 answers
3k views

Solutions to C# 5 async re-entrancy

So, something's been bugging me about the new async support in C# 5: The user presses a button which starts an async operation. The call returns immediately and the message pump starts running again -...
Nick Butler's user avatar
16 votes
3 answers
11k views

How will C# 5 async support help UI thread synchronization issues?

I heard somewhere that C# 5 async-await will be so awesome that you will not have to worry about doing this: if (InvokeRequired) { BeginInvoke(...); return; } // do your stuff here It looks ...
Alex's user avatar
  • 3,228
16 votes
4 answers
3k views

Uses of persistent data structures in non-functional languages

Languages that are purely functional or near-purely functional benefit from persistent data structures because they are immutable and fit well with the stateless style of functional programming. But ...
Ray Toal's user avatar
  • 1,305
16 votes
5 answers
15k views

What are the best resources for learning about concurrency and multi-threaded applications? [closed]

I realised I have a massive knowledge gap when it comes to multi-threaded applications and concurrent programming. I've covered some basics in the past, but most of it seems to be gone from my mind, ...
Zepee's user avatar
  • 277

1
2 3 4 5
14