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
503
votes
22 answers

The application may be doing too much work on its main thread

I am new to Android SDK/API environment. It's the first I am trying to draw a plot/chart. I tried running different kinds of sample codes on the emulator using 3 different free libraries, nothing is showing on the layout screen. The logcat is…
user2038135
  • 5,051
  • 3
  • 13
  • 6
501
votes
17 answers

How can one use multi threading in PHP applications

Is there a realistic way of implementing a multi-threaded model in PHP whether truly, or just simulating it. Some time back it was suggested that you could force the operating system to load another instance of the PHP executable and handle other…
Steve Obbayi
  • 6,015
  • 5
  • 27
  • 24
500
votes
24 answers

Timeout on a function call

I'm calling a function in Python which I know may stall and force me to restart the script. How do I call the function or what do I wrap it in so that if it takes longer than 5 seconds the script cancels it and does something else?
Teifion
  • 108,121
  • 75
  • 161
  • 195
460
votes
16 answers

What is the meaning of the term "thread-safe"?

Does it mean that two threads can't change the underlying data simultaneously? Or does it mean that the given code segment will run with predictable results when multiple threads are executing that code segment?
459
votes
15 answers

How to check if current thread is not main thread

I need to check if the thread running a certain piece of code is the main (UI) thread or not. How can I achieve this?
Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
456
votes
27 answers

How to wait for all threads to finish, using ExecutorService?

I need to execute some amount of tasks 4 at a time, something like this: ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } //...wait for completion somehow How can I get notified…
serg
  • 109,619
  • 77
  • 317
  • 330
447
votes
9 answers

What is std::promise?

I'm fairly familiar with C++11's std::thread, std::async and std::future components (e.g. see this answer), which are straight-forward. However, I cannot quite grasp what std::promise is, what it does and in which situations it is best used. The…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
443
votes
6 answers

What's the difference between Invoke() and BeginInvoke()

Just wondering what the difference between BeginInvoke() and Invoke() are? Mainly what each one would be used for. EDIT: What is the difference between creating a threading object and calling invoke on that and just calling BeginInvoke() on a…
Nathan W
  • 54,475
  • 27
  • 99
  • 146
439
votes
2 answers

What is the difference between asynchronous programming and multithreading?

I thought that they were basically the same thing — writing programs that split tasks between processors (on machines that have 2+ processors). Then I'm reading this, which says: Async methods are intended to be non-blocking operations. An…
435
votes
8 answers

What is the difference between task and thread?

In C# 4.0, we have Task in the System.Threading.Tasks namespace. What is the true difference between Thread and Task. I did some sample program(help taken from MSDN) for my own sake of learning with Parallel.Invoke Parallel.For Parallel.ForEach…
user372724
433
votes
7 answers

When to use AtomicReference in Java?

When do we use AtomicReference? Is it needed to create objects in all multithreaded programs? Provide a simple example where AtomicReference should be used.
Chintu
  • 4,333
  • 3
  • 15
  • 4
431
votes
3 answers

Task vs Thread differences

There are two classes available in .NET: Task and Thread. What is the difference between those classes? When is it better to use Thread over Task (and vice-versa)?
Jacek
  • 11,661
  • 23
  • 69
  • 123
430
votes
7 answers

Handling InterruptedException in Java

What is the difference between the following ways of handling InterruptedException? What is the best way to do it? try{ //... } catch(InterruptedException e) { Thread.currentThread().interrupt(); } OR try{ //... } catch(InterruptedException…
softwarematter
  • 28,015
  • 64
  • 169
  • 263
429
votes
15 answers

The calling thread cannot access this object because a different thread owns it

My code is as below public CountryStandards() { InitializeComponent(); try { FillPageControls(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Country Standards", MessageBoxButton.OK,…
Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86
425
votes
15 answers

What is a semaphore?

A semaphore is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a semaphore and how do you use it?
bmurphy1976
  • 29,564
  • 11
  • 33
  • 24