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
421
votes
23 answers

Is there an advantage to use a Synchronized Method instead of a Synchronized Block?

Can any one tell me the advantage of synchronized method over synchronized block with an example?
Warrior
  • 39,156
  • 44
  • 139
  • 214
421
votes
26 answers

Java: notify() vs. notifyAll() all over again

If one Googles for "difference between notify() and notifyAll()" then a lot of explanations will pop up (leaving apart the javadoc paragraphs). It all boils down to the number of waiting threads being waken up: one in notify() and all in…
Sergey Mikhanov
  • 8,880
  • 9
  • 44
  • 54
419
votes
11 answers

Threading pool similar to the multiprocessing Pool?

Is there a Pool class for worker threads, similar to the multiprocessing module's Pool class? I like for example the easy way to parallelize a map function def long_running_func(p): c_func_no_gil(p) p = multiprocessing.Pool(4) xs =…
Martin
  • 12,408
  • 6
  • 34
  • 30
419
votes
17 answers

How to use background thread in swift?

How to use threading in swift? dispatchOnMainThread:^{ NSLog(@"Block Executed On %s", dispatch_queue_get_label(dispatch_get_current_queue())); }];
Anshul
  • 4,386
  • 3
  • 12
  • 11
418
votes
7 answers

time.sleep -- sleeps thread or process?

In Python for *nix, does time.sleep() block the thread or the process?
Jeremy Dunck
  • 5,724
  • 6
  • 25
  • 30
416
votes
17 answers

How do you kill a Thread in Java?

How do you kill a java.lang.Thread in Java?
flybywire
  • 261,858
  • 191
  • 397
  • 503
411
votes
9 answers

Could you explain STA and MTA?

Can you explain STA and MTA in your own words? Also, what are apartment threads and do they pertain only to COM? If so, why?
John
  • 5,381
  • 4
  • 30
  • 26
409
votes
7 answers

Do zombies exist ... in .NET?

I was having a discussion with a teammate about locking in .NET. He's a really bright guy with an extensive background in both lower-level and higher-level programming, but his experience with lower level programming far exceeds mine. Anyway, He…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
407
votes
22 answers

Avoid synchronized(this) in Java?

Whenever a question pops up on SO about Java synchronization, some people are very eager to point out that synchronized(this) should be avoided. Instead, they claim, a lock on a private reference is to be preferred. Some of the given reasons…
eljenso
  • 16,789
  • 6
  • 57
  • 63
402
votes
12 answers

How many threads is too many?

I am writing a server, and I send each action of into a separate thread when the request is received. I do this because almost every request makes a database query. I am using a threadpool library to cut down on construction/destruction of…
ryeguy
  • 65,519
  • 58
  • 198
  • 260
402
votes
15 answers

How do I run a simple bit of code in a new thread?

I have a bit of code that I need to run in a different thread than the GUI as it currently causes the form to freeze whilst the code runs (10 seconds or so). Assume I have never created a new thread before; what's a simple/basic example of how to…
Tim M
398
votes
13 answers

Handler vs AsyncTask vs Thread

I got slightly confused about the differences between Handlers, AsyncTask and Threads in Android. I've read quite a few blogs and questions here in StackOverflow. Handler are background threads that provide you to communicate with the UI. Updating…
Alx
  • 6,275
  • 7
  • 32
  • 54
396
votes
3 answers

multiprocessing.Pool: When to use apply, apply_async or map?

I have not seen clear examples with use-cases for Pool.apply, Pool.apply_async and Pool.map. I am mainly using Pool.map; what are the advantages of others?
Phyo Arkar Lwin
  • 6,673
  • 12
  • 41
  • 55
391
votes
16 answers

Running code in main thread from another thread

In an android service I have created thread(s) for doing some background task. I have a situation where a thread needs to post certain task on main thread's message queue, for example a Runnable. Is there a way to get Handler of the main thread and…
Ahmed
  • 14,503
  • 22
  • 92
  • 150
390
votes
7 answers

Simple example of threading in C++

Can someone post a simple example of starting two (Object Oriented) threads in C++. I'm looking for actual C++ thread objects that I can extend run methods on (or something similar) as opposed to calling a C-style thread library. I left out any OS…
Zak
  • 24,947
  • 11
  • 38
  • 68