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
904
votes
27 answers

What is a daemon thread in Java?

Can anybody tell me what daemon threads are in Java?
rocker
  • 11,317
  • 7
  • 22
  • 12
823
votes
11 answers

Service vs IntentService in the Android platform

I am seeking an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)? I also believe that an IntentService runs in a different thread and a Service does not. So, as far as I can see,…
roiberg
  • 13,629
  • 12
  • 60
  • 91
800
votes
25 answers

What is the volatile keyword useful for?

At work today, I came across the volatile keyword in Java. Not being very familiar with it, I found this explanation. Given the detail in which that article explains the keyword in question, do you ever use it or could you ever see a case in which…
Richard
  • 9,972
  • 4
  • 26
  • 32
796
votes
29 answers

How should I unit test multithreaded code?

I have thus far avoided the nightmare that is testing multi-threaded code since it just seems like too much of a minefield. I'd like to ask how people have gone about testing code that relies on threads for successful execution, or just how people…
jkp
  • 78,960
  • 28
  • 103
  • 104
777
votes
10 answers

Volatile vs. Interlocked vs. lock

Let's say that a class has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented. To increment this field, which approach should be used, and why? lock(this.locker)…
core
  • 32,451
  • 45
  • 138
  • 193
670
votes
22 answers

Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

I have a scenario. (Windows Forms, C#, .NET) There is a main form which hosts some user control. The user control does some heavy data operation, such that if I directly call the UserControl_Load method the UI become nonresponsive for the duration…
Prerak K
  • 10,940
  • 7
  • 30
  • 37
615
votes
27 answers

How to get the return value from a thread?

The function foo below returns a string 'foo'. How can I get the value 'foo' which is returned from the thread's target? from threading import Thread def foo(bar): print('hello {}'.format(bar)) return 'foo' thread = Thread(target=foo,…
wim
  • 338,267
  • 99
  • 616
  • 750
614
votes
10 answers

When to use Task.Delay, when to use Thread.Sleep?

Are there good rule(s) for when to use Task.Delay versus Thread.Sleep? Specifically, is there a minimum value to provide for one to be effective/efficient over the other? Lastly, since Task.Delay causes context-switching on a async/await state…
Tom K.
  • 6,549
  • 3
  • 14
  • 14
578
votes
11 answers

What is the difference between ManualResetEvent and AutoResetEvent in .NET?

I have read the documentation on this and I think I understand. An AutoResetEvent resets when the code passes through event.WaitOne(), but a ManualResetEvent does not. Is this correct?
Ben McNiel
  • 8,661
  • 10
  • 36
  • 38
565
votes
14 answers

The difference between the Runnable and Callable interfaces in Java

What is the difference between using the Runnable and Callable interfaces when designing a concurrent thread in Java, why would you choose one over the other?
Scottm
  • 7,004
  • 8
  • 32
  • 33
548
votes
20 answers

Programmatically find the number of cores on a machine

Is there a way to determine how many cores a machine has from C/C++ in a platform-independent way? If no such thing exists, what about determining it per-platform (Windows/*nix/Mac)?
hazzen
  • 17,128
  • 6
  • 41
  • 33
536
votes
16 answers

Undefined reference to pthread_create in Linux

I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/ #include #include #define NUM_THREADS 5 void *PrintHello(void *threadid) { long tid; tid = (long)threadid; …
Ralph
  • 6,249
  • 7
  • 23
  • 19
534
votes
18 answers

Why is lock(this) {...} bad?

The MSDN documentation says that public class SomeObject { public void SomeOperation() { lock(this) { //Access instance variables } } } is "a problem if the instance can be accessed publicly". I'm wondering why? Is it…
Anton
  • 6,860
  • 12
  • 30
  • 26
528
votes
14 answers

What is the purpose of Looper and how to use it?

I am new to Android. I want to know what the Looper class does and also how to use it. I have read the Android Looper class documentation but I am unable to completely understand it. I have seen it in a lot of places but unable to understand its…
Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
505
votes
7 answers

std::unique_lock or std::lock_guard?

I have two use cases. A. I want to synchronise access to a queue for two threads. B. I want to synchronise access to a queue for two threads and use a condition variable because one of the threads will wait on content to be stored into the queue by…
chmike
  • 20,922
  • 21
  • 83
  • 106