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
323
votes
12 answers

How to pause / sleep thread or process in Android?

I want to make a pause between two lines of code, Let me explain a bit: -> the user clicks a button (a card in fact) and I show it by changing the background of this button: thisbutton.setBackgroundResource(R.drawable.icon); -> after let's say 1…
Hubert
  • 16,012
  • 18
  • 45
  • 51
322
votes
15 answers

ThreadStart with parameters

How do you start a thread with parameters in C#?
JL.
  • 78,954
  • 126
  • 311
  • 459
321
votes
10 answers

How to pass parameters to ThreadStart method in Thread?

How to pass parameters to Thread.ThreadStart() method in C#? Suppose I have method called 'download' public void download(string filename) { // download code } Now I have created one thread in the main method: Thread thread = new Thread(new…
Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75
320
votes
11 answers

How to make a function wait until a callback has been called using node.js

I have a simplified function that looks like this: function(query) { myApi.exec('SomeCommand', function(response) { return response; }); } Basically i want it to call myApi.exec, and return the response that is given in the callback lambda.…
Chris
  • 39,719
  • 45
  • 189
  • 235
316
votes
9 answers

How to properly stop the Thread in Java?

I need a solution to properly stop the thread in Java. I have IndexProcessorclass which implements the Runnable interface: public class IndexProcessor implements Runnable { private static final Logger LOGGER =…
Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
311
votes
10 answers

Why must wait() always be in synchronized block

We all know that in order to invoke Object.wait(), this call must be placed in synchronized block, otherwise an IllegalMonitorStateException is thrown. But what's the reason for making this restriction? I know that wait() releases the monitor, but…
diy
  • 3,590
  • 3
  • 19
  • 16
311
votes
10 answers

multiprocessing vs multithreading vs asyncio

I found that in Python 3.4, there are few different libraries for multiprocessing/threading: multiprocessing vs threading vs asyncio. But I don't know which one to use or is the "recommended one". Do they do the same thing, or are different? If so,…
310
votes
4 answers

std::lock_guard or std::scoped_lock?

C++17 introduced a new lock class called std::scoped_lock. Judging from the documentation it looks similar to the already existing std::lock_guard class. What's the difference and when should I use it?
Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
302
votes
9 answers

Does ruby have real multithreading?

I know about the "cooperative" threading of ruby using green threads. How can I create real "OS-level" threads in my application in order to make use of multiple cpu cores for processing?
skolima
  • 31,963
  • 27
  • 115
  • 151
302
votes
12 answers

How to debug a single thread in Visual Studio?

I have a solution with some projects. There are several break-points in different projects. I want to trace the first thread hit one of these break-points and continue tracing that single thread despite of other threads entering the same…
Xaqron
  • 29,931
  • 42
  • 140
  • 205
302
votes
4 answers

Difference between volatile and synchronized in Java

I am wondering at the difference between declaring a variable as volatile and always accessing the variable in a synchronized(this) block in Java? According to this article http://www.javamex.com/tutorials/synchronization_volatile.shtml there is a…
Albus Dumbledore
  • 12,368
  • 23
  • 64
  • 105
302
votes
18 answers

Maximum number of threads per process in Linux?

What is the maximum number of threads that can be created by a process under Linux? How (if possible) can this value be modified?
user60008667
302
votes
4 answers

Difference between thread's context class loader and normal classloader

What is the difference between a thread's context class loader and a normal class loader? That is, if Thread.currentThread().getContextClassLoader() and getClass().getClassLoader() return different class loader objects, which one will be used?
abracadabra
  • 3,067
  • 3
  • 17
  • 7
299
votes
7 answers

Difference between a "coroutine" and a "thread"?

What are the differences between a "coroutine" and a "thread"?
jldupont
  • 93,734
  • 56
  • 203
  • 318
297
votes
6 answers

What is the difference between Task.Run() and Task.Factory.StartNew()

I have Method : private static void Method() { Console.WriteLine("Method() started"); for (var i = 0; i < 20; i++) { Console.WriteLine("Method() Counter = " + i); Thread.Sleep(500); } …
Sergiy Lichenko
  • 3,102
  • 2
  • 11
  • 10