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
33
votes
6 answers

Running code on the main thread from a secondary thread?

This is a general Java question and not an Android one first off! I'd like to know how to run code on the main thread, from the context of a secondary thread. For example: new Thread(new Runnable() { public void run() { //work…
Javawag
  • 1,547
  • 2
  • 16
  • 23
33
votes
5 answers

iOS How to determine what is blocking the UI

I'm fairly new to iOS development but I'm starting to grasp some of the more complicated concepts. I currently have an application that implements an AVCam to capture video. The AVCam is created on a separate thread, but uses a view that is in my…
user379468
  • 3,989
  • 10
  • 50
  • 68
33
votes
1 answer

Embedded Jetty why to use join

An example taken from an Embedded Jetty tutorial suggests the following: public static void main(String[] args) throws Exception { Server server = new Server(8080); server.setHandler(new HelloHandler()); server.start(); …
Loner shushman
  • 757
  • 3
  • 8
  • 18
33
votes
6 answers

Thread was being aborted

I am using Server.Transfer. Everything works fine, but exception log shows following exception. System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object…
Syed Tayyab Ali
  • 3,643
  • 7
  • 31
  • 36
33
votes
3 answers

What is the best way to pass information between threads?

I want to be listening to a server while my program is doing other things, when a message is received from the server I want to interpret it. I know about threading but not sure completely on how it works. If I have a thread listening for the…
Bakies
  • 333
  • 1
  • 3
  • 4
33
votes
3 answers

Monitor vs WaitHandle based thread sync

I was under the impression, after reading this article that it is better to use Monitor/Lock for thread synchronisation as it does not use native resources Specific quote (from page 5 of the article): Monitor.Wait/Pulse isn't the only way of…
Matt
  • 2,984
  • 1
  • 24
  • 31
33
votes
5 answers

Thread safe queue - Enqueue / Dequeue

Firstly, i'll explain a short scenario; As a signal from certain devices triggers, an object of type Alarm is added to a queue. At an interval, the queue is checked, and for each Alarm in the queue, it fires a method. However, the problem i'm…
Kestami
  • 2,045
  • 3
  • 32
  • 47
33
votes
2 answers

Application.Exit() vs Application.ExitThread() vs Environment.Exit()

I am trying to figure out which I should be using. On closing my WinForm app fires of a Form in Dialog mode. That form runs a Background worker that Syncs the DB with the remote DB and displays it's progress on the "Splash Form." I have a method…
Refracted Paladin
  • 12,096
  • 33
  • 123
  • 233
33
votes
2 answers

Why does ConcurrentDictionary.GetOrAdd(key, valueFactory) allow the valueFactory to be invoked twice?

I am using a concurrent dictionary as a thread-safe static cache and noticed the following behavior: From the MSDN docs on GetOrAdd: If you call GetOrAdd simultaneously on different threads, addValueFactory may be called multiple times, but its…
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
33
votes
5 answers

Execute code on main thread in Android without access to an Activity?

I have an Android service that starts and maintains a background thread. From time to time, the background thread needs to do a callback on the main thread. I'm stumped as to how to do this. I can't call this.runOnUiThread because "this" is an…
Frank LaRosa
  • 3,533
  • 6
  • 26
  • 32
33
votes
9 answers

Unit testing a multithreaded application?

Does anyone have any advice for a consistent way to unit test a multithreaded application? I have done one application where our mock "worker threads" had a thread.sleep with a time that was specified by a public member variable. We would use this…
Marcus King
  • 1,667
  • 3
  • 15
  • 20
33
votes
8 answers

How to avoid deadlocks?

When using multiple threads, shared memory needs to be locked by critical sections. However, using critical sections causes potential deadlocks. How can they be avoided?
Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
32
votes
5 answers

When to use volatile and synchronized

I know there are many questions about this, but I still don't quite understand. I know what both of these keywords do, but I can't determine which to use in certain scenarios. Here are a couple of examples that I'm trying to determine which is the…
Stripies
  • 1,267
  • 5
  • 19
  • 29
32
votes
8 answers

C Programming: Debugging with pthreads

One of the hardest things for me to initially adjust to was my first intense experience programming with pthreads in C. I was used to knowing exactly what the next line of code to be run would be and most of my debugging techniques centered around…
JoeCool
  • 4,392
  • 11
  • 50
  • 66
32
votes
4 answers

C++ std::atomic vs. Boost atomic

In my application, I have an int and a bool variable, which are accessed (multiple write/read) by multiple threads. Currently, I am using two mutexes, one for int and one for bool to protect those variables. I heard about using atomic variables and…
2607
  • 4,037
  • 13
  • 49
  • 64