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
388
votes
7 answers

What's the difference between deadlock and livelock?

Can somebody please explain with examples (of code) what is the difference between deadlock and livelock?
macindows
  • 4,303
  • 4
  • 18
  • 9
387
votes
11 answers

How to run a Runnable thread in Android at defined intervals?

I developed an application to display some text at defined intervals in the Android emulator screen. I am using the Handler class. Here is a snippet from my code: handler = new Handler(); Runnable r = new Runnable() { public void run() { …
Rajapandian
  • 9,257
  • 24
  • 74
  • 86
383
votes
32 answers

Using module 'subprocess' with timeout

Here's the Python code to run an arbitrary command returning its stdout data, or raise an exception on non-zero exit codes: proc = subprocess.Popen( cmd, stderr=subprocess.STDOUT, # Merge stdout and stderr stdout=subprocess.PIPE, …
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
383
votes
5 answers

Start thread with member function

I am trying to construct a std::thread with a member function that takes no arguments and returns void. I can't figure out any syntax that works - the compiler complains no matter what. What is the correct way to implement spawn() so that it returns…
abergmeier
  • 13,224
  • 13
  • 64
  • 120
379
votes
8 answers

Why use a ReentrantLock if one can use synchronized(this)?

I'm trying to understand what makes the lock in concurrency so important if one can use synchronized (this). In the dummy code below, I can do either: synchronized the entire method or synchronize the vulnerable area (synchronized(this){...}) OR…
adhg
  • 10,437
  • 12
  • 58
  • 94
366
votes
11 answers

If async-await doesn't create any additional threads, then how does it make applications responsive?

Time and time again, I see it said that using async-await doesn't create any additional threads. That doesn't make sense because the only ways that a computer can appear to be doing more than 1 thing at a time is Actually doing more than 1 thing at…
Ms. Corlib
  • 4,993
  • 4
  • 12
  • 19
366
votes
12 answers

Getting the thread ID from a thread

In C# when debugging threads for example, you can see each thread's ID. I couldn't find a way to get that same thread, programmatically. I could not even get the ID of the current thread (in the properties of the Thread.currentThread). So, I wonder…
LolaRun
  • 5,526
  • 6
  • 33
  • 45
364
votes
11 answers

When should the volatile keyword be used in C#?

Can anyone provide a good explanation of the volatile keyword in C#? Which problems does it solve and which it doesn't? In which cases will it save me the use of locking?
Doron Yaacoby
  • 9,412
  • 8
  • 48
  • 59
354
votes
11 answers

What is a "thread" (really)?

I have been trying to find a good definition, and get an understanding, of what a thread really is. It seems that I must be missing something obvious, but every time I read about what a thread is, it's almost a circular definition, a la "a thread is…
richard
  • 12,263
  • 23
  • 95
  • 151
352
votes
7 answers

What is the difference between atomic / volatile / synchronized?

How do atomic / volatile / synchronized work internally? What is the difference between the following code blocks? Code 1 private int counter; public int getNextUniqueIndex() { return counter++; } Code 2 private AtomicInteger counter; public…
hardik
  • 9,141
  • 7
  • 32
  • 48
350
votes
5 answers

C# version of java's synchronized keyword?

Does c# have its own version of the java "synchronized" keyword? I.e. in java it can be specified either to a function, an object or a block of code, like so: public synchronized void doImportantStuff() { // dangerous code goes…
Soraz
  • 6,610
  • 4
  • 31
  • 48
338
votes
19 answers

How can I pass a parameter to a Java Thread?

Can anyone suggest to me how I can pass a parameter to a thread? Also, how does it work for anonymous classes?
Steve
  • 21,163
  • 21
  • 69
  • 92
335
votes
13 answers

What resources are shared between threads?

Recently, I have been asked a question in an interview what's the difference between a process and a thread. Really, I did not know the answer. I thought for a minute and gave a very weird answer. Threads share the same memory, processes do not.…
Xinus
  • 29,617
  • 32
  • 119
  • 165
330
votes
16 answers

Why doesn't JavaScript support multithreading?

Is it a deliberate design decision or a problem with our current day browsers which will be rectified in the coming versions?
Niyaz
  • 53,943
  • 55
  • 151
  • 182
324
votes
17 answers

What is the difference between concurrency, parallelism and asynchronous methods?

Concurrency is having two tasks run in parallel on separate threads. However, asynchronous methods run in parallel but on the same 1 thread. How is this achieved? Also, what about parallelism? What are the differences between these 3 concepts?
GurdeepS
  • 65,107
  • 109
  • 251
  • 387