Here's a preview from my zine, Bite Size Linux!! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!
read the transcript!
Threads let a process do many different things at the same time
process:
thread 1: I’m calculating ten million digits of π! so fun!
thread 2: I’m finding a REALLY BIG prime number!
threads in the same process share memory
thread 1: I’ll write some digits of to π O x 129420 in memory thread 2: uh oh! that’s where I was putting my prime numbers.
and they share code
calculate-pi
find-big-prime-number
but each thread has its own stack and they can be run by different CPUs at the same time
CPU 1: π thread
CPU 2: primes thread
sharing memory can cause problems (race conditions!)
at the same time:
memory: 23
thread 1: I’m going to add 1 to that number!
thread 2: I’m going to add 1 to that number!
RESULT: 24
WRONG. Should be 25!
why use threads instead of starting a new process?
a thread takes less time to create.
sharing data between threads is very easy. But it’s also easier to make mistakes with threads.
thread 1: you weren’t supposed to CHANGE that data!
Saturday Morning Comics!
Want another comic like this in your email every Saturday? Sign up here!