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
33
votes
2 answers

Use cases for ithreads (interpreter threads) in Perl and rationale for using or not using them?

If you want to learn how to use Perl interpreter threads, there's good documentation in perlthrtut (threads tutorial) and the threads pragma manpage. It's definitely good enough to write some simple scripts. However, I have found little guidance on…
Lumi
  • 14,775
  • 8
  • 59
  • 92
33
votes
4 answers

Grand Central Dispatch vs NSThreads?

I searched a variety of sources but don't really understand the difference between using NSThreads and GCD. I'm completely new to the OS X platform so I might be completely misinterpreting this. From what I read online, GCD seems to do the exact…
fdh
  • 5,256
  • 13
  • 58
  • 101
33
votes
4 answers

Asynchronous processing or message queues in PHP (CakePHP)

I am building a website in CakePHP that processes files uploaded though an XML-RPC API and though a web frontend. Files need to be scanned by ClamAV, thumbnails need to be generated, et cetera. All resource intensive work that takes some time for…
Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
33
votes
2 answers

Is it possible to force an existing Java application to use no more than x cores?

We are benchmarking existing Java programs. They are threaded applications designed to benefit from multi-core CPUs. We would like to measure the effect of the number of cores on the running speed, but we are unwilling (and unable) to change the…
Daniel Lemire
  • 3,470
  • 2
  • 25
  • 23
33
votes
4 answers

How does the JVM terminate daemon threads? or How to write daemon threads that terminate gracefully

Hypothetical scenario: I have a daemon thread responsible for some I/O, the main thread finishes and returns, and the JVM decides to terminate my daemon thread. How does it do so? Interrupt? Finalize? How can I code my daemon thread so that it…
Aaron J Lang
  • 2,048
  • 3
  • 20
  • 27
33
votes
3 answers

How Linux handles threads and process scheduling

I'm trying to understand how Linux handles process scheduling and thread scheduling. I read that Linux can schedule both processes and threads. Does Linux have a thread scheduler AND a process scheduler? If yes, how do they cooperate?
whitefox
  • 955
  • 2
  • 10
  • 7
33
votes
4 answers

How to create a thread/Task with a continuous loop?

I am looking for the correct way/structure to create a loop in a Thread/Task... The reason for this is, i need to check the DB every 15sec for report requests. This is what i tried so far, but i get OutOfMemoryException: private void…
Willem
  • 9,166
  • 17
  • 68
  • 92
33
votes
4 answers

How can I monitor/log Tomcat's thread pool?

I have a Tomcat installation where I suspect the thread pool may be decreasing over time due to threads not being properly released. I get an error in catalina.out when maxthreads is reached, but I would like to log the number of threads in use to a…
Alex P
  • 333
  • 1
  • 3
  • 4
33
votes
5 answers

AsyncTask.executeOnExecutor() before API Level 11

The normal way we do AsyncTask in Android is, from Android API: private class DoIntenseTask extends AsyncTask { protected Void doInBackground(Object... params) { for (Object param : params) { Object rtnObj =…
yorkw
  • 40,926
  • 10
  • 117
  • 130
33
votes
7 answers

Exception when using FolderBrowserDialog

I'm getting the following Exception when trying to use FolderBrowserDialog: System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has…
Daniel
  • 6,595
  • 9
  • 38
  • 70
33
votes
7 answers

Threading in Java: How to lock an object?

The following Function is executing in its own thread: private void doSendData() { try { //writeToFile(); // just a temporary location of a call InetAddress serverAddr = InetAddress.getByName(serverAddress); …
Niko Gamulin
  • 66,025
  • 95
  • 221
  • 286
33
votes
4 answers

Concurrency: Are Python extensions written in C/C++ affected by the Global Interpreter Lock?

One of Python's strongest points is the ease of writing C and C++ extensions to speed up processor intensive parts of the code. Can these extensions avoid the Global Interpreter Lock or are they also restricted by the GIL? If not, then this "ease of…
user75770
33
votes
6 answers

Background task in Scala

I have a cache I want to periodically check and prune. In Java, I'd do the following: new Thread(new Runnable() { void run() { while (true) { Thread.sleep(1000); // clear the cache's old entries } } }).start(); Sure, I'd…
Jeb
  • 15,939
  • 6
  • 34
  • 37
33
votes
5 answers

Terminate multiple threads when any thread completes a task

I am new to both python, and to threads. I have written python code which acts as a web crawler and searches sites for a specific keyword. My question is, how can I use threads to run three different instances of my class at the same time. When one…
user446836
  • 733
  • 4
  • 16
  • 24
33
votes
3 answers

ThreadPool max threads

I've got some trouble with .NET's ThreadPool (.NET 4). I've read that by default .NET has a limit of 25 threads per processor, but according to forum posts on SO and on other places, I can increase the limit with the below code. void SetThreads(int…
foxy
  • 7,599
  • 2
  • 30
  • 34