Questions tagged [multithreading]

For questions regarding multi-threading, the ability of a computer or a program to perform work concurrently or asynchronously by utilizing multiple concurrent streams of execution (generally referred to as threads).

Multi-threading is a common model to implement SM in multi-cored machines, where threads share memory through shared variables. In this model in parallel programming a processor can create multiple threads that will be executed in parallel. Each thread has its own stack, variables and ID, considered private state. For every thread there is a unique heap shared by all, then considered shared memory.

In order to allow a volume of work to be most effectively and safely divided into multiple concurrent streams of execution, a number of critical areas need to be addressed.

  • Scheduling: ensure worker tasks are able to progress independently and effectively (e.g. deadlock/livelock avoidance)
  • Publication: ensure data altered in one thread is only visible to others as expected
  • Synchronization: ensure critical regions are protected from multiple concurrent updates causing data loss/corruption.

The underlying tenet of concurrent processing is Amdahl's law (graph). This law governs the diminishing amount of throughput that can be achieved by greater numbers of concurrent processors/cores. Therefore the overall aim of multi-threading is to minimize the amount of serial execution (exclusive locking) within any concurrent system.

Frequently Asked Questions

Books

More information:

Related tags

139166 questions
34
votes
2 answers

C# equivalent to Java's Thread.setDaemon?

How do I set a thread to a daemon thread in C#?
Epaga
  • 38,231
  • 58
  • 157
  • 245
34
votes
4 answers

How to cleanly exit a threaded C++ program?

I am creating multiple threads in my program. On pressing Ctrl-C, a signal handler is called. Inside a signal handler, I have put exit(0) at last. The thing is that sometimes the program terminates safely but the other times, I get runtime error…
rjlion795
  • 515
  • 1
  • 5
  • 16
34
votes
11 answers

Java ExecutorService: awaitTermination of all recursively created tasks

I use an ExecutorService to execute a task. This task can recursively create other tasks which are submitted to the same ExecutorService and those child tasks can do that, too. I now have the problem that I want to wait until all the tasks are done…
Christoph
  • 687
  • 1
  • 6
  • 12
34
votes
2 answers

How do the C++ STL (ExecutionPolicy) algorithms determine how many parallel threads to use?

C++17 upgraded 69 STL algorithms to support parallelism, by the use of an optional ExecutionPolicy parameter (as the 1st argument). eg. std::sort(std::execution::par, begin(v), end(v)); I suspect the C++17 standard deliberately says nothing about…
Scott Smedley
  • 1,779
  • 20
  • 28
34
votes
4 answers

Is Ruby's stdlib Logger class thread-safe?

In short, is the standard library Logger class in Ruby thread-safe? Only useful info Google turned up was someone on a forum saying it "seems" thread-safe. And I don't feel like spending time testing a logger to try to figure out if it is or…
jimeh
  • 1,391
  • 1
  • 16
  • 32
34
votes
3 answers

C++: Static variables in multithreaded program

What is the problem with having static variables (especially within functions) in multithreaded programs? Thanks.
user542687
34
votes
5 answers

Using string as a lock to do thread synchronization

While i was looking at some legacy application code i noticed it is using a string object to do thread synchronization. I'm trying to resolve some thread contention issues in this program and was wondering if this could lead so some strange…
Illuminati
  • 4,539
  • 2
  • 35
  • 55
34
votes
7 answers

Do mutexes guarantee ordering of acquisition? Unlocking thread takes it again while others are still waiting

A coworker had an issue recently that boiled down to what we believe was the following sequence of events in a C++ application with two threads: Thread A holds a mutex. While thread A is holding the mutex, thread B attempts to lock it. Since it is…
Jason R
  • 11,159
  • 6
  • 50
  • 81
34
votes
7 answers

How to properly release an AVCaptureSession

I'm using the AVFoundation classes to capture the live video stream from the camera and to process the video samples. This works nicely. However, I do have problems properly releasing the AVFoundation instances (capture session, preview layer, input…
Codo
  • 75,595
  • 17
  • 168
  • 206
34
votes
4 answers

Nvidia graphics driver causing noticeable frame stuttering

Ok I've been researching this issue for a few days now so let me go over what I know so far which leads me to believe this might be an issue with NVidia's driver and not my code. Basically my game starts stuttering after running a few seconds…
TylerGlaiel
  • 652
  • 7
  • 16
34
votes
5 answers

Android Instrumentation Testing - UI Thread Issues

I am trying to write an Instrumentation Test for my Android app. I'm running into some weird threading issues and I can't seem to find a solution. My Original Test: @RunWith(AndroidJUnit4.class) public class WorkOrderDetailsTest { @Rule …
Khalos
  • 2,335
  • 1
  • 23
  • 38
34
votes
5 answers

Thread.Sleep(0) : What is the normal behavior?

To my understanding a Thread.Sleep(0) force a context switch on the OS. I wanted to check what was the maximum amount of time that could pass in an application before to receive some CPU time. So I built an application that does Thread.Sleep(0) in a…
Benoittr
  • 4,091
  • 4
  • 27
  • 38
34
votes
5 answers

difference between Thread and Handler

Can somebody tell me the deference between Thread and Handler? When we use Thread and when we use Handler? I have two code in my project , But I can't understand them. final Handler handler = new Handler() { @Override public void…
Khodayar
  • 391
  • 1
  • 4
  • 13
34
votes
1 answer

Threading error when using `ActiveRecord with_connection do` & ActionController::Live

Major edit: Since originally finding this issue I have whittled it down to the below. I think this is now a marginally more precise description of the problem. Comments on the OP may therefore not correlate entirely. Edit lightly modified version…
34
votes
2 answers

What is the Java Equivalent of C# "Logical Call Context"

In .net, there is an "uber" thread-local-storage (TLS) which allows arbitrary TLS data to auto-magically "jump" from one thread to another. It is based on the CallContext class. In other words, a logical request can spawn a hierarchy of new…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234