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

Speed Up gganimate Rendering

I am currently creating a gif which contains large data. I want it in high resolution. On my old PC it took hours to render and simply wasn't worth it. My new PC has a very strong intel i9-9900k core processor which has sped it up to about 30…
user3456588
  • 609
  • 4
  • 9
33
votes
2 answers

What's the recommended scoped_session usage pattern in a multithreaded sqlalchemy webapp?

I'm writing an application with python and sqlalchemy-0.7. It starts by initializing the sqlalchemy orm (using declarative) and then it starts a multithreaded web server - I'm currently using web.py for rapid prototyping but that could change in the…
Luke404
  • 10,282
  • 3
  • 25
  • 31
33
votes
5 answers

ThreadPoolExecutor: how to limit the queue maxsize?

I am using ThreadPoolExecutor class from the concurrent.futures package def some_func(arg): # does some heavy lifting # outputs some results from concurrent.futures import ThreadPoolExecutor with ThreadPoolExecutor(max_workers=1) as…
Bob
  • 5,809
  • 5
  • 36
  • 53
33
votes
1 answer

Explanation of the Thread-Local Handshakes

As part of the putative JDK 10 this JEP 312: Thread-Local Handshakes was proposed. I've tried to grasp its description, but I am still not confident that I got the idea properly. Is it essentially an attempt to reanimate something similar to the…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
33
votes
4 answers

Is WCF ClientBase thread safe?

I have implemented ClientBase to use WCF to connect to a service. I'm then calling a method on the channel to communicate with the service. base.Channel.CalculateSomething(); Is this call thread safe or should I lock around it when running multiple…
Hnas
  • 331
  • 1
  • 3
  • 3
33
votes
3 answers

DecimalFormat.format(double) in different threads

I have to print many formatted decimal values in many threads in parallel. To format the decimal values I use a java.text.DecimalFormat configured by a pattern. I am aware of the warning from the java doc of DecimalFormat: Decimal formats are…
Ralph
  • 118,862
  • 56
  • 287
  • 383
33
votes
2 answers

Why is async considered better performing than multithreading?

I understand both asynchronous and multithreaded programming and I have done both and can do them with ease. However one thing still bugs me: why is the general consensus that async is better performing than multithreading? (added: I'm talking about…
Vilx-
  • 104,512
  • 87
  • 279
  • 422
33
votes
2 answers

WPF BackgroundWorker vs. Dispatcher

In my WPF application I need to do an async-operation then I need to update the GUI. And this thing I have to do many times in different moment with different oparations. I know two ways to do this: Dispatcher and BackgroundWorker. Because when I…
lamarmora
  • 1,116
  • 4
  • 16
  • 32
33
votes
1 answer

What is a database connection, technically?

When we say we have a "database connection" or a "connection pool that has several connections open", on the technical level, what are we meaning actually? My understanding is: A database connection is a link to a thread running in the…
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
33
votes
2 answers

How does "Compare And Set" in AtomicInteger works

AtomicInteger works with two concepts : CAS and volatile variable. Using volatile variable insures that the current value will be visible to all threads and it will not be cached. But I am confused over CAS(compare AND set) concept which is…
Onki
  • 1,879
  • 6
  • 38
  • 58
33
votes
4 answers

Do two synchronized methods execute simultaneously

I have 4 methods (m1, m2, m3 and m4) in a class. Method m1, m2 and m3 are synchronized methods. Also, I have 4 threads t1, t2, t3 and t4 respectively. If t1 access the m1 method (synchronized method), could t2 thread access m2 method (synchronized…
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75
33
votes
4 answers

Can OpenMP be used for GPUs?

I've been searching the web but I'm still very confused about this topic. Can anyone explain this more clearly? I come from an Aerospace Engineering background (not from a Computer Science one), so when I read online about OpenMP/CUDA/etc. and…
André Almeida
  • 379
  • 1
  • 4
  • 11
33
votes
6 answers

How to get non-current thread's stacktrace?

It is possible to get stacktrace using System.Diagnostics.StackTrace, but thread has to be suspended. Suspend and Resume function are obsolete, so I expect that better way exists.
bh213
  • 6,343
  • 9
  • 43
  • 52
33
votes
1 answer

Why is std::weak_ptr::expired optimized away?

In the following code, while ( !Ref.expired() ); is joyfully optimized into an infinite loop. If the line of code is changed to while ( !Ref.lock() );. everything works as expected. So two questions really: 1) How can the compiler optimize away…
Puff Of Hot Air
  • 331
  • 2
  • 4
33
votes
3 answers

WPF Databinding thread safety?

Well lets say i have an object that i databind to, it implements INotifyPropertyChanged to tell the GUI when a value has changed... if i trigger this from a different thread than the GUI thread how would wpf behave? and will it make sure that it…
Peter
  • 37,042
  • 39
  • 142
  • 198