Questions tagged [multithreading]

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

Filter by
Sorted by
Tagged with
132 votes
3 answers
59k 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?" ...
user avatar
  • 2,566
103 votes
13 answers
73k 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 ...
78 votes
4 answers
87k 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 ...
user avatar
  • 1,689
70 votes
3 answers
56k 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,...
user avatar
  • 1,406
68 votes
6 answers
26k 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 ...
user avatar
68 votes
1 answer
19k 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 ...
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 ...
user avatar
  • 1,267
60 votes
1 answer
27k 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 ...
user avatar
  • 4,761
59 votes
10 answers
23k 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 ...
user avatar
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 ...
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 ...
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 ...
user avatar
42 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 ...
user avatar
  • 1,194
40 votes
5 answers
13k 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 ...
user avatar
  • 14.6k
40 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 ...
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 ...
user avatar
  • 7,502
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 ...
user avatar
  • 4,948
37 votes
3 answers
28k 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.
user avatar
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 ...
user avatar
  • 3,534
35 votes
2 answers
15k 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'...
user avatar
  • 4,288
35 votes
8 answers
20k 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...
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 ...
user avatar
33 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 ...
user avatar
  • 585
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 ...
user avatar
29 votes
3 answers
55k 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 ...
user avatar
28 votes
2 answers
9k 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() ...
user avatar
27 votes
10 answers
22k 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 ...
user avatar
26 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 ...
user avatar
25 votes
4 answers
10k 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 ...
user avatar
25 votes
7 answers
18k 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 ...
user avatar
  • 15.4k
24 votes
9 answers
55k 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 ...
user avatar
  • 350
24 votes
3 answers
17k 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, ...
user avatar
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 ...
user avatar
  • 20.7k
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 ...
user avatar
21 votes
19 answers
18k 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) ...
21 votes
3 answers
14k 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-...
user avatar
  • 1,349
19 votes
5 answers
28k 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 ...
user avatar
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 (...
user avatar
  • 801
18 votes
7 answers
17k 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 ...
user avatar
  • 339
18 votes
3 answers
20k 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 ...
user avatar
18 votes
3 answers
2k 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 ...
user avatar
  • 335
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 ...
user avatar
  • 359
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-...
user avatar
  • 1,861
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 -...
user avatar
16 votes
3 answers
10k 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 ...
user avatar
  • 3,208
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 ...
user avatar
  • 1,177
16 votes
5 answers
14k 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, ...
user avatar
  • 277
16 votes
3 answers
4k 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 ...
user avatar
15 votes
4 answers
23k views

What is a zombie process or thread?

What is a zombie process or thread, and what creates them? Do I just kill them, or can I do something to get diagnostics about how they died?
user avatar
  • 4,948

1
2 3 4 5
14