Questions tagged [multithreading]

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

Filter by
Sorted by
Tagged with
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 ...
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 ...
  • 14.8k
79 votes
4 answers
89k 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 ...
  • 1,709
32 votes
3 answers
59k 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 ...
13 votes
7 answers
2k views

What constitutes proper use of threads in programming?

I am tired of hearing people recommend that you should use only one thread per processor, while many programs use up to 100 per process! take for example some common programs vb.net ide uses about 25 ...
  • 643
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 (...
  • 811
9 votes
1 answer
8k views

How are queues made thread-safe in Python

I admit this was asked to me in interview a long time ago, but I never bothered to check it. The question was simple, how does Python make Queue thread-safe? My answer was, because of Interpreter ...
2 votes
1 answer
2k views

Multi-threading on a single core

Hypothetically, if you (I / someone) were to create a multi-threaded application, utilising the processing powers of multiple cores (for example, using TPL in C#). Would that program crash, if it were ...
72 votes
3 answers
60k 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,...
  • 1,423
61 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 ...
  • 4,819
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 ...
  • 1,277
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 ...
  • 819
38 votes
3 answers
29k 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.
26 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 ...
21 votes
3 answers
16k 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-...
  • 1,359
19 votes
5 answers
29k 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 ...
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 ...
  • 1,265
15 votes
4 answers
7k views

Is It "Wrong"/Bad Design To Put A Thread/Background Worker In A Class?

I have a class that will read from Excel (C# and .Net 4) and in that class I have a background worker that will load the data from Excel while the UI can remain responsive. My question is as follows: ...
  • 5,143
13 votes
1 answer
4k views

Best practise is not to poll...but isn't polling happening internally anyway when a thread calls wait()?

Say we have some thread that wants to check when another thread is finished its task. I have read that we should call a wait() type function that will make this thread wait until it receives a ...
  • 231
11 votes
1 answer
532 views

Drawing thread interaction

I'd like to draw (pen and pencils) threads interaction in a UML(-like) notation. I don't insist on UML, anything that is obvious to the reader should do. I started with sequence diagrams, but I don't ...
  • 1,291
9 votes
4 answers
16k views

Is it a performance hit to create threads that loop a lot to check for things?

This is a generic question that I've always wondered. In general, is it intensive for a CPU to create threads that perform "while not true ..." loops or similar? For example, suppose I do: // ...
8 votes
4 answers
11k views

Threads or ThreadPool? Fixed or Dynamic ThreadPool?

I have a java program which listens on a port for input. Based on Input, it calls a webservice and then returns a success/failure back to the client program. I fork a thread for each client ...
  • 441
8 votes
2 answers
4k views

Mutex vs Semaphore: How to implement them _not_ in terms of the other?

Recently I had to implement a Semaphore using a Mutex and a Conditional Variable (this combination is also known as a Monitor) for an exercise at the university: the Semaphore's decrement operation ...
  • 4,579
8 votes
2 answers
5k views

Concurrency pattern of logger in multithreaded application

The context: We are working on a multi-threaded (Linux-C) application that follows a pipeline model. Each module has a private thread and encapsulated objects which do processing of data; and each ...
  • 10.5k
7 votes
1 answer
4k views

Should I use a boolean or a cancellation-token to stop this task?

I have a program that starts a task as follows: token = this.tokenSource.Token; var t = Task.Factory.StartNew(() => { while(!token.IsCancellationRequested) { // do the work... ...
  • 2,718
6 votes
2 answers
3k views

Pthread scheduling (System and Process)

I've read the operating system concepts 8th edition written by abraham silberschatz. However, I don't understand Pthread Scheduling. Thread-library schedules user-level-threads on LWP(Lightweight ...
  • 241
6 votes
6 answers
2k views

Am I looking at multithreading the wrong way? (Java)

This is a re-post from my question in StackOverflow, so here goes: For the past few weeks now I've been studying Concurrency(Multithreading) in Java. I find it difficult and rather different than ...
  • 179
6 votes
5 answers
2k views

Performance: Is There a Reason to Use Processes over Threads?

This is a general question but I work in the .NET world so I'd like to know if there any specific quirks about the .NET Framework / Core platforms that I should be concerned about here. I think it's ...
5 votes
2 answers
17k views

JUnit Testing in Multithread Application

This is a problem me and my team faces in almost all of the projects. Testing certain parts of the application with JUnit is not easy and you need to start early and to stick to it, but that's not the ...
5 votes
1 answer
13k views

Is there a limit on the number of threads that can be spawned simultaneously?

Yesterday I came across this question: How can i call robocopy within a python script to bulk copy multiple folders?, and I though it might be a good exercise for multithreading. I though of spawning ...
  • 225
5 votes
2 answers
917 views

are multithreading, multiprocessing, multitasking implemented at instruction set or OS?

On a computer with a single cpu core, must a multithreading program be implemented based on an OS? Can it be implemented in the instruction set and run without an OS? Can multiple programs run in ...
  • 5,375
5 votes
3 answers
1k views

What are the so-called "levels" of understanding multithreading?

I seem to remember reading somewhere some list of 4 "levels" of understanding multithreading. This may have been in a formal publication, or it may have been in an extremely informal context (even ...
  • 1,201
5 votes
1 answer
7k views

Implementation of Asynchronous function call in C

I took it upon myself recently to work towards making a general-purpose Translator to automatically translate js programs into C, but I got stuck when thinking of asynchronous behaviour. Using ANSI / ...
5 votes
1 answer
265 views

Evaluating concurrent application design approaches on Linux

Working on relatively low-speed hardware, I need to grab a snapshot of a rapidly changing datastream and then post-process the snapshot data and save it to disk. I need help with designing the ...
  • 289
4 votes
1 answer
2k views

Is there a formula to figure out the total execution time of sequential asynchronous processes?

I have a set of steps that need to run sequentially over a list of data. However each step only operates on a single piece of data at a time. Because of this, I can run the steps asynchronously. For ...
4 votes
3 answers
2k views

Internal Mutation of Persistent Data Structures

To clarify, when I mean use the terms persistent and immutable on a data structure, I mean that: The state of the data structure remains unchanged for its lifetime. It always holds the same data, ...
  • 1,723
4 votes
2 answers
2k views

Threads - When to use barrier?

Using python syntax, threading.Barrier(NUM_THREADS) establishes a control point and all participating threads block until all of the participating “parties” have reached that point. It lets threads ...
  • 2,255
4 votes
4 answers
4k views

Testing concurrency/thread-safety

A program I have wrote uses multiple threads and I believe my program is thread-safe but how can I really know? I've read a number of examples online and none of them describe how to test the code is ...
  • 143
4 votes
2 answers
6k views

What's the simplest way to deal with events in C in this situation?

I'm writing a text-based game, in C. I want the game to handle NPCs movement and actions in the background, while the user is making his moves. I'd also use time-based events to tell the user when ...
3 votes
4 answers
4k views

Is the singleton pattern prone to thread safety problems?

Is the singleton pattern prone to thread safety problems? If so, what are the best methods to work around them?
  • 4,958
3 votes
5 answers
12k views

Sharing buffer between multiple threads

I had a job process that was executing a lot of IO to read and write temporary files. Now I want to (need to) reduce the quantity of IO executions. So I want to create a sort of circular buffer ...
  • 141
2 votes
2 answers
470 views

How is it possible to have limitless availability of threads while only having a finite number of physical processing units?

So I'm having trouble understanding the relationship between threads (software) and processor cores (hardware). Basically my understanding of a processor core is a unit that deals with commands and ...
  • 2,204
2 votes
1 answer
5k views

WCF service - best way to accept requests from a client and do background work?

For a project at work, I was tasked to create a WCF service that will receive email messages from client and queue them on a database, while at the same time the service will check the DB every 30 ...
  • 785
1 vote
3 answers
3k views

Is it possible to perform mutual exclusion of threads without OS support?

I suppose you could adapt distributed mutual exclusion algorithms (Ricart/Agrawala, etc), but aside from these more exotic approaches, are you required to use primitives provided by the OS in order to ...
1 vote
1 answer
232 views

Good strategy to implement a listener processing tasks provided by the database

Let's assume this conceptual scenario: The database is constantly populated with "a ton" of tasks named PleaseSendAnEmail. A scheduler/listener in background is aimed to handle those tasks. Of ...
  • 3,818
1 vote
0 answers
43 views

Push instead of pull on a list that might change due to a command in addition to events? [closed]

I need to show a refreshable/searchable list of items and update/add-to it whenever an event is published. How do I make sure no events are lost while refreshing/searching is happening? Some sort of ...
  • 4,829
1 vote
0 answers
136 views

Trade off between concurrency and stagnation in a Dining Philosophers problem analogue?

I am currently facing a problem in the application I am developing that I think is the same as, or an analogue of, the Dining Philosophers Problem. In my application I have a list of N resources. I ...
  • 185
1 vote
3 answers
1k views

Is there an algorithm to determine the amount of work needed to keep workers busy?

I wrote a multi-threaded program that randomly generates strings, and after generating, checks to see if the string contains a certain value. Namely, StringGenerator and StringChecker. What I'm trying ...
  • 2,383
0 votes
1 answer
3k views

I/O bound or CPU-bound

I have a telephony application, there is a windows service(third party product) running on a remote server. The application runs against the server. To connect the server, we have the code. public ...
  • 223
0 votes
3 answers
1k views

Multiprocessing vs multithreading

During my last job interview which was about half year ago, the technical guy asked me, whether the better option is to use e.g. 100 processes to do some work, or only one process but with 100 threads....
  • 285