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
32
votes
4 answers

Thread.sleep() implementation

Today I had an interview on which I asked candidate quite usual and basic question about the difference between Thread.sleep() and Object.wait(). I expected him to answer something like like this, but he said these methods basically are the same…
wax
  • 2,400
  • 3
  • 19
  • 28
32
votes
2 answers

How does Intel TBB's scalable_allocator work?

What does the tbb::scalable_allocator in Intel Threading Building Blocks actually do under the hood ? It can certainly be effective. I've just used it to take 25% off an apps' execution time (and see an increase in CPU utilization from ~200% to…
timday
  • 24,582
  • 12
  • 83
  • 135
32
votes
2 answers

What happened to thread.start_new_thread in python 3

I liked the ability to turn a function into a thread without the unnecessary line to define a class. I know about _thread, however it appears that you are not supposed to use _thread. Is there a good-practice equivalent of thread.start_new_thread…
lemiant
  • 4,205
  • 4
  • 31
  • 38
32
votes
5 answers

How do I manage ruby threads so they finish all their work?

I have a computation that can be divided into independent units and the way I'm dealing with it now is by creating a fixed number of threads and then handing off chunks of work to be done in each thread. So in pseudo code here's what it looks like #…
David K.
  • 6,153
  • 10
  • 47
  • 78
32
votes
12 answers

Best practices for Java logging from multiple threads?

I want to have a diagnostic log that is produced by several tasks managing data. These tasks may be in multiple threads. Each task needs to write an element (possibly with subelements) to the log; get in and get out quickly. If this were a…
Jason S
  • 184,598
  • 164
  • 608
  • 970
32
votes
3 answers

Is there any optimization for thread safety in for loop of Java?

I have a snippet of code that change a counter in two threads. It's not thread safe because I didn't put any atomic variable or lock in the code. It gives the right result as I expected if the code only run once, but I want to run it for several…
Kidsunbo
  • 1,024
  • 1
  • 11
  • 21
32
votes
5 answers

What's the best way to update an ObservableCollection from another thread?

I am using the BackgroundWorker to update an ObservableCollection but it gives this error: "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread." What's the best and…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
32
votes
10 answers

"Closing" a blocking queue

I’m using java.util.concurrent.BlockingQueue in a very simple producer-consumer scenario. E.g. this pseudo code depicts the consumer part: class QueueConsumer implements Runnable { @Override public void run() { while(true) …
Lachezar Balev
  • 11,498
  • 9
  • 49
  • 72
32
votes
6 answers

Within a C# instance method, can 'this' ever be null?

I have a situation where very rarely a Queue of Objects is dequeuing a null. The only call to Enqueue is within the class itself: m_DeltaQueue.Enqueue(this); Very rarely, a null is dequeued from this queue in the following code (a static…
Derrick
  • 2,502
  • 2
  • 24
  • 34
32
votes
2 answers

The workers in ThreadPoolExecutor is not really daemon

The thing I cannot figure out is that although ThreadPoolExecutor uses daemon workers, they will still run even if main thread exit. I can provide a minimal example in python3.6.4: import concurrent.futures import time def fn(): while True: …
Sraw
  • 18,892
  • 11
  • 54
  • 87
32
votes
8 answers

What's the maximum number of threads in Windows Server 2003?

Does anyone know? And a bigger question is what happens when you encounter this maximum? Is this the same number with other Windows OSs such as Vista, XP etc.?
Douglas Anderson
  • 4,652
  • 10
  • 40
  • 49
32
votes
3 answers

Can atomics suffer spurious stores?

In C++, can atomics suffer spurious stores? For example, suppose that m and n are atomics and that m = 5 initially. In thread 1, m += 2; In thread 2, n = m; Result: the final value of n should be either 5 or 7, right? But could it…
thb
  • 13,796
  • 3
  • 40
  • 68
32
votes
2 answers

Is it possible to create an atomic vector or array in C++?

I have some code which uses an array of int (int[]) in a thread which is activated every second. I use lock() from std::mutex to lock this array in this thread. However I wonder if there is a way to create an atomic array (or vector) to avoid using…
rainbow
  • 1,161
  • 3
  • 14
  • 29
32
votes
1 answer

thread_local static member template definition: initialisation fails with gcc

When a static member in a C++ class is both thread_local and a member template, it doesn't get initialised. #include #include class A { public: template thread_local static std::unordered_map
maiphi
  • 461
  • 3
  • 9
32
votes
5 answers

Is a string property itself threadsafe?

String's in C# are immutable and threadsafe. But what when you have a public getter property? Like this: public String SampleProperty{ get; private set; } If we have two threads and the first is calling 'get' and the second is calling 'set'…
TomTom
  • 992
  • 2
  • 10
  • 26
1 2 3
99
100