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
3 answers

Examples/Illustration of Wait-free And Lock-free Algorithms

I've read that wait-free causes all threads to finish independently and lock-free ensures the program as a whole completes. I couldn't quite get it. Can anyone give an example (java) illustrating this. EDIT: Does lock-free mean a program without…
softwarematter
  • 28,015
  • 64
  • 169
  • 263
32
votes
2 answers

Rust mpsc::Sender cannot be shared between threads?

I thought the whole purpose of a channel was to share data between threads. I have this code, based on this example: let tx_thread = tx.clone(); let ctx = self; thread::spawn(|| { ... let result = ctx.method() …
Christian Grabowski
  • 2,782
  • 3
  • 32
  • 57
32
votes
5 answers

objc_sync_enter / objc_sync_exit not working with DISPATCH_QUEUE_PRIORITY_LOW

I need a read\write lock for my application. I've read https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock and wrote my own class, cause there are no read/write lock in swift class ReadWriteLock { var logging = true var b = 0 …
Yegor Razumovsky
  • 902
  • 2
  • 9
  • 26
32
votes
3 answers

onPostExecute on cancelled AsyncTask

Does onPostExecute execute if the AsyncTask has been cancelled? If it does execute, is it safe to say that I should always ask if the task has been cancelled (isCancelled) at the start of onPostExecute, before doing anything else?
hpique
  • 119,096
  • 131
  • 338
  • 476
32
votes
3 answers

What is the purpose of passing parameter to synchronized block?

I know that When you synchronize a block of code, you specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more…
John Rambo
  • 906
  • 1
  • 17
  • 37
32
votes
3 answers

Thread vs Threadstart

In C#, practically, I haven't observed any difference between the following: new Thread(SomeMethod).Start(); , new Thread(new ParameterizedThreadStart(SomeMethod)); and new Thread(new ThreadStart(SomeMethod)); What is the difference, if there is…
Shashank sarma
  • 423
  • 1
  • 4
  • 8
32
votes
3 answers

Stopping long-sleep threads

Let's suppose I have a thread which should perform some task periodically but this period is 6 times each hour 12 times each hour (every 5 minutes), I've often seen code which controls the thread loop with a is_running flag which is checked every…
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
32
votes
5 answers

ConcurrentBag Vs List

What is the advantage of using a ConcurrentBag(Of MyType) against just using a List(Of MyType)? The MSDN page on the CB states that ConcurrentBag(Of T) is a thread-safe bag implementation, optimized for scenarios where the same thread will …
Ben
  • 3,926
  • 12
  • 54
  • 87
32
votes
4 answers

python asyncio, how to create and cancel tasks from another thread

I have a python multi-threaded application. I want to run an asyncio loop in a thread and post calbacks and coroutines to it from another thread. Should be easy but I cannot get my head around the asyncio stuff. I came up to the following solution…
32
votes
2 answers

What does 'context' exactly mean in C# async/await code?

Lets looks at some simple C# async/await code where I have an object reference (obj) before and after an await with ConfigureAwait(false) private async Task AnAsyncLibraryMethod(SomeObject obj) { …
DeepSpace101
  • 13,110
  • 9
  • 77
  • 127
32
votes
3 answers

Is a variable swap guaranteed to be atomic in python?

With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe I wanted to know if the following: (x, y) = (y, x) will be guaranteed atomic in cPython. (x and y are both python…
dhruvbird
  • 6,061
  • 6
  • 34
  • 39
32
votes
6 answers

Asynctask vs Thread vs Services vs Loader

I got slightly confused about the differences between Asynctask, Thread, Service, Loader in Android. I know how it works. But i still don't understand what and when should i use. I work with Android for 3 years, and generally still use AsyncTask…
32
votes
2 answers

OpenMP vs C++11 threads

In the following example the C++11 threads take about 50 seconds to execute, but the OMP threads only 5 seconds. Any ideas why? (I can assure you it still holds true if you are doing real work instead of doNothing, or if you do it in a different…
user2588666
  • 821
  • 1
  • 7
  • 16
32
votes
15 answers

Advice for converting a large monolithic singlethreaded application to a multithreaded architecture?

My company's main product is a large monolithic C++ application, used for scientific data processing and visualisation. Its codebase goes back maybe 12 or 13 years, and while we have put work into upgrading and maintaining it (use of STL and Boost…
David
  • 13,360
  • 7
  • 66
  • 130
32
votes
5 answers

Multi Threading

I'm learning Multi Threading at the moment, in C#, but as with all learning I like to learn best practices. At the moment the area seems fuzzy. I understand the basics, and I can create threads. What should I look out for when creating multi…
James Jeffery
  • 12,093
  • 19
  • 74
  • 108
1 2 3
99
100