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

Synchronization on the local variables

I have a multithreaded Java code in which: several threads read stateful objects from the synchronized shared storage (i.e. as a result of this, some threads may reference the same objects); each thread then invokes a method process() and passes…
Sergey Mikhanov
  • 8,880
  • 9
  • 44
  • 54
34
votes
6 answers

If you unlock an already unlocked mutex, is the behavior undefined?

If you unlock an already unlocked mutex, is the behavior unsafe, safe, or undefined? The purpose of the question is related to the following code, where I don't know if it would be better to unlock the mutexes within the if block, or just outside…
Crazy Chenz
  • 12,650
  • 12
  • 50
  • 62
34
votes
4 answers

What is the difference between Go's multithreading and pthread or Java Threads?

What is the difference between Go's multithreading approach and other approaches, such as pthread, boost::thread or Java Threads?
Frank
  • 64,140
  • 93
  • 237
  • 324
34
votes
4 answers

Throwing exceptions in callback method for Timers

I was unable to find an answer to this question anywhere... What happens with the exceptions thrown in the callback method for System.Threading.Timer, (or in the event handler for System.Timers.Timer). Is the exception propagated to the thread on…
Kornelije Petak
  • 9,412
  • 15
  • 68
  • 96
34
votes
7 answers

How can I tell reliably if a boost thread has exited its run method?

I assumed joinable would indicate this, however, it does not seem to be the case. In a worker class, I was trying to indicate that it was still processing through a predicate: bool isRunning(){return thread_->joinable();} Wouldn't a thread that…
JeffV
  • 52,985
  • 32
  • 103
  • 124
34
votes
3 answers

Dispatch queues: How to tell if they're running and how to stop them

I'm just playing around with GCD and I've written a toy CoinFlipper app. Here's the method that flips the coins: - (void)flipCoins:(NSUInteger)nFlips{ // Create the queues for work dispatch_queue_t mainQueue = dispatch_get_main_queue(); …
Abizern
  • 146,289
  • 39
  • 203
  • 257
34
votes
5 answers

Why "implements Runnable" is Preferred over "extends Thread"?

The Java Thread itself implements a Java Runnable! and according to most of the experts over Internet, implements Runnable is preferred over extends Thread! even though we cannot use utilize Runnable in the sense of thread with out the Thread…
Tariq
  • 2,489
  • 11
  • 36
  • 61
34
votes
7 answers

Get Task CancellationToken

Can I get CancellationToken which was passed to Task constructor during task action executing. Most of samples look like this: CancellationTokenSource cts = new CancellationTokenSource(); CancellationToken token = cts.Token; Task myTask =…
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
34
votes
1 answer

Are thread pools needed for pure Haskell code?

In Real World Haskell, Chapter 28, Software transactional memory, a concurrent web link checker is developed. It fetches all the links in a webpage and hits every once of them with a HEAD request to figure out if the link is active. A concurrent…
Nicolas Dudebout
  • 9,172
  • 2
  • 34
  • 43
34
votes
5 answers

AtomicInteger and volatile

I know volatile allows for visibility, AtomicInteger allows for atomicity. So if I use a volatile AtomicInteger, does it mean I don't have to use any more synchronization mechanisms? Eg. class A { private volatile AtomicInteger count; …
Achow
  • 8,600
  • 6
  • 39
  • 49
34
votes
7 answers

Is there a way to indefinitely pause a thread?

I've been working on a web crawling .NET app in my free time, and one of the features of this app that I wanted to included was a pause button to pause a specific thread. I'm relatively new to multi-threading and I haven't been able to figure out a…
Dan Herbert
  • 99,428
  • 48
  • 189
  • 219
34
votes
7 answers

Which is threadsafe atomic or non atomic?

I searched and found immutable are thread safe while mutable is not. This is fine. But i got misleading notes, blogs, answers about atomic vs non-atomic about thread safety, kindly give an explanation for the answer. Suppose there is an atomic…
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
34
votes
3 answers

Can Web Workers utilize 100% of a multi-core CPU?

I've been trying to find out just how capable web workers are of distributing processor load. I've yet to find any demos that seem to be able to get my quad core 2600k to even 50%, let alone 100%. Here's a web worker demo I've tried to max my CPU…
BumbleShrimp
  • 2,150
  • 23
  • 42
34
votes
1 answer

ASP.NET IIS - when are requests queued?

The following article by Thomas Marquardt describes how IIS handles ASP.Net requests, the max/min CLR worker threads/Managed IO threads that can be configured to run, the various requests queues involved and their default sizes. Now as per the…
coder_bro
  • 10,503
  • 13
  • 56
  • 88
34
votes
5 answers

What is java's equivalent of ManualResetEvent?

What is java's equivalent of ManualResetEvent?
ripper234
  • 222,824
  • 274
  • 634
  • 905