Click here to Skip to main content
15,889,776 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
JokeRe: Pakistan has an army of dinosaurs! Pin
Afzaal Ahmad Zeeshan29-Jun-20 5:13
professionalAfzaal Ahmad Zeeshan29-Jun-20 5:13 
GeneralRe: Pakistan has an army of dinosaurs! Pin
CodeWraith29-Jun-20 6:13
CodeWraith29-Jun-20 6:13 
GeneralRe: Pakistan has an army of dinosaurs! Pin
RedDk29-Jun-20 7:06
RedDk29-Jun-20 7:06 
GeneralRe: Pakistan has an army of dinosaurs! Pin
CodeWraith29-Jun-20 7:26
CodeWraith29-Jun-20 7:26 
GeneralRe: Pakistan has an army of dinosaurs! Pin
RedDk29-Jun-20 8:32
RedDk29-Jun-20 8:32 
GeneralRe: Pakistan has an army of dinosaurs! Pin
Kris321129-Jun-20 22:35
professionalKris321129-Jun-20 22:35 
GeneralAdventures in Async Pin
Jörgen Andersson29-Jun-20 0:02
professionalJörgen Andersson29-Jun-20 0:02 
GeneralRe: Adventures in Async PinPopular
OriginalGriff29-Jun-20 0:12
mveOriginalGriff29-Jun-20 0:12 
Oh yes, as soon as your thread count exceeds the core count, you are going to get some slowdown.

You need to be aware that threading is not a "magic bullet" that will solve all your performance woes at a stroke - it needs to be carefully though about and planned, or it can do two things:
1) Slow your machine to a crawl, and make your application considerably slower than it started out.
2) Crash or lock up your app completely.

The reasons why are simple:
1) Threads require two things to run: memory and a free core. The memory will be at the very least the size of a system stack in your language (usually around 1MB for Windows, 8MB for Linux) plus some overhead for the thread itself and yet more for any memory based objects each thread creates; and a thread can only run when a core becomes available. If you generate more threads than you have cores then most of them will spend a lot of time sitting waiting for a thread to be available.
The more threads you generate, the worse problems become: more threads puts more load on the system to switch threads more often and that takes core time as well. All threads ion the system form all processes share the cores in the machine, so other apps and System threads also need their time to run. Add too many, and the system will spend more and more of it's time trying to work out which thread to run and performance degrades. Generate enough threads to exceed the physical memory in your computer and performance suddenly takes an enormous hit as the virtual memory system comes in and starts thrashing memory pages to the HDD.

2) Multiple threads within a process have to be thread safe because they share memory and other resources - which means that several things can happen:
2a) If two threads need the same resource then you can easily end up in a situation where thread A has locked resource X and wants Y, while thread B has locked resource Y and wants X. At this point a "deadly embrace" has occurred and no other thread (nor any other that need X or Y can run ever again.
2b) If your code isn't thread safe, then different threads can try to read and / or alter the same memory at the same time: this often happens when trying to add or remove items from a collection. At this point strange things start to happen up to and including your app crashing.
2c) If resources have a finite capacity - like the bandwidth on an internet connection for example - then bad threading can easily use it all - at either end of the link. If you run out of capacity, your threads will stall waiting for it (and everybody else using the connection will also suffer). If the other end runs out of capacity it may stutter, slow down, crash, or assume that you are a DDOS attack and take precautions.

You can't just go "multithread this" and assume it will work: it's something that needs very, very careful planning.
Think about it this way: if you have a very large bus it is a slow way to get from A to B, but when you average it out over the large number of passengers it's pretty quick. But if you put each passenger in a separate car in theory they can all get there faster - except you are putting a lot more vehicles on the same roads which means more chance of traffic jams, accidents, breakdowns, and so forth. Put too many on the same roads and they get blocked up with cars and nobody can move anywhere because there is a car in their way ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!

GeneralRe: Adventures in Async Pin
Jörgen Andersson29-Jun-20 0:26
professionalJörgen Andersson29-Jun-20 0:26 
GeneralRe: Adventures in Async Pin
OriginalGriff29-Jun-20 0:36
mveOriginalGriff29-Jun-20 0:36 
GeneralRe: Adventures in Async Pin
Kirk Hawley29-Jun-20 7:52
professionalKirk Hawley29-Jun-20 7:52 
GeneralRe: Adventures in Async Pin
Leo5629-Jun-20 21:31
Leo5629-Jun-20 21:31 
GeneralRe: Adventures in Async Pin
dandy7229-Jun-20 5:24
dandy7229-Jun-20 5:24 
PraiseRe: Adventures in Async Pin
Member 1467846130-Jun-20 6:41
Member 1467846130-Jun-20 6:41 
GeneralRe: Adventures in Async Pin
Kornfeld Eliyahu Peter29-Jun-20 0:13
professionalKornfeld Eliyahu Peter29-Jun-20 0:13 
GeneralRe: Adventures in Async Pin
Jörgen Andersson29-Jun-20 0:15
professionalJörgen Andersson29-Jun-20 0:15 
GeneralRe: Adventures in Async Pin
Kornfeld Eliyahu Peter29-Jun-20 0:35
professionalKornfeld Eliyahu Peter29-Jun-20 0:35 
GeneralRe: Adventures in Async Pin
Jörgen Andersson29-Jun-20 0:52
professionalJörgen Andersson29-Jun-20 0:52 
GeneralRe: Adventures in Async Pin
Richard Deeming29-Jun-20 0:30
mveRichard Deeming29-Jun-20 0:30 
GeneralRe: Adventures in Async Pin
Jörgen Andersson29-Jun-20 0:38
professionalJörgen Andersson29-Jun-20 0:38 
GeneralRe: Adventures in Async Pin
#realJSOP29-Jun-20 2:18
mve#realJSOP29-Jun-20 2:18 
GeneralRe: Adventures in Async Pin
Greg Utas29-Jun-20 0:42
professionalGreg Utas29-Jun-20 0:42 
GeneralRe: Adventures in Async Pin
Jörgen Andersson29-Jun-20 0:46
professionalJörgen Andersson29-Jun-20 0:46 
GeneralRe: Adventures in Async Pin
Garth J Lancaster29-Jun-20 1:04
professionalGarth J Lancaster29-Jun-20 1:04 
GeneralRe: Adventures in Async Pin
Ron Anders29-Jun-20 3:16
Ron Anders29-Jun-20 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.