Lecture 24: Parallelism & Threading
CSE 373: Data Structures and Algorithms
1
CSE 373 25WI
CSE 373 25WI
1
Warm Up
Imagine again you are a chef in a restaurant, now you want to come up with as many ways as possible to speed up the way in which you are able to cook many different dishes all at one time?
CSE 373 25WI
2
Announcements
CSE 373 25WI
3
Concurrency vs Parallelism
§parallelism refers to running things simultaneously on separate resources (ex. Separate CPUs)
§concurrency refers to running multiple threads on a shared resources
§Concurrency is one person cooking multiple dishes at the same time.
§Parallelism is having multiple people (possibly cooking the same dish).
§Allows processes to run ‘in the background’
§Responsiveness – allow GUI to respond while computation happens
§CPU utilization – allow CPU to compute while waiting (waiting for data, for input)
§isolation – keep threads separate so errors in one don’t affect the others
compilers
CSE 373 25WI
4
malloc in C is like “new” in Java
Asks the OS to partition memory space in the current stack frame. We call this space in the frame the “heap”
Instructions that act on variables get transformed into machine code and stored at the bottom of memory
Global variables and static variables get stored in a special place in the frame since they need to stick around the entire process
Local variables get added to the “stack” within the frame when called then pop off the stack at the end of the method since we don’t need them anymore
CSE 373 25WI
5
What is a Thread?
CSE 373 25WI
6
What is a Thread?
In most modern OS’s threads are the unit of scheduling.
Cohabit the same RAM address space
Disadvantages:
Advantages:
CSE 373 25WI
7
What is a Thread?
Chrome treats tabs as different processes
CSE 373 25WI
8
Processes and Threads
Heap
Memory
Registers
Stack
Thread
Registers
Stack
Thread
Registers
Stack
Thread
Process
CSE 373 25WI
9
Sequential Programming
Only one “task” is being processed at a time
Even while processing one query, the CPU is idle the vast majority of the time
At most one I/O operation is being processed at a time
CSE 373 25WI
10
CSE 373 23SP
11
CSE 373 25WI
11
How does a computer execute a program?
Core 1
Core 3
Core 2
Core 4
CSE 373 25WI
12
How does the CPU handle tasks?
Core 1
Core 3
Core 2
Core 4
CSE 373 25WI
13
Parallelism
A search engine could run concurrently:
Use multiple “workers” aka threads
CSE 373 25WI
14
Parallelism Example
Goal: find the sum of an array
Sequential approach: start at index 0 and sum one by one until the end of the array
Parallel approach: 4 processors will each find the sum of ¼ of the array then add those 4 results together
CSE 373 25WI
15
Parallelism Example Pseudocode
int sum(int[] arr) {
SumThread[] res = new SumThread[4];
len = arr.length;
for (int i = 0; i < 4; i++) {
res[i] = sumRange(arr, i*len/4, (i+1_*len/4);
res[i].start();
}
int ans = 0;
for (int i = 0; i < 4; i++) {
res[i].join();
ans += res[i].ans;
}
return ans;
}
SumThread sumRange(int[] arr, int lo, int high) {
result = 0;
for (int j = lo; j < hi; j++) {
result += arr[j];
}
return result;
}
join() causes program to pause until the other thread completes its run method to await its result
SumThread object is constructed with run() method override to indicate the work to happen when thread is triggered. In this case, summing up the values within the array.
start() creates a new thread and then triggers the run() method
CSE 373 25WI
16
Possible issues
CSE 373 25WI
17
Concurrency
Parallelism and concurrency are similar but technically different techniques
CSE 373 25WI
18
Concurrency Challenges
public class Bank {
private int amount;
...
public boolean withdraw(int amount) {
if (this.amount < amount) {
return false;
}
this.amount -= amount;
return true;
}
}
A: 1
if (this.amount < amount) {
B: 1
if (this.amount < amount) {
A: 3
this.amount -= amount;
1
2
�3
4
A: 4
return true;
B: 3
this.amount -= amount;
B: 4
return true;
B
account.withdraw(100);
A
account.withdraw(150);
Main
Bank acc = new Bank(200);
Thread A -> acc.withdraw(150);
Thread B -> acc.withdraw(100);
CSE 373 25WI
19
Concurrency
CSE 373 25WI
20
Locks / Mutexes
public class Bank {
private int amount;
private Lock lock;
...
public boolean withdraw(int amount) {
lock.lock(); // Blocks until free
if (this.amount < amount) {
return false;
}
this.amount -= amount;
return true;
lock.unlock(); // Frees the lock
}
}
CSE 373 25WI
21
Is everything all good?
Locks are great for ensuring synchronization, but can lead to their own problems…
Suppose I had a program with two resources (e.g., a user account and a post they’ve made). And want separate locks for each to allow methods that only need to access one lock on one.
Are there any potential problems here?
public void editPost() {
userLock.lock();
postLock.lock();
// Edit the post
userLock.unlock();
postLock.unlock();
}
public void deletePost() {
postLock.lock();
userLock.lock();
// Delete the post
postLock.unlock();
userLock.unlock();
}
CSE 373 25WI
22
Deadlocking
CSE 373 25WI
23
Helpful Resources
What is a Thread (youtube)
Threading Tutorial - Concurrency, Threading and Parallelism (youtube)
Parallelism and Concurrency Text Chapters (by Prof Grossman)
Process vs Thread (youtube)
CSE 373 25WI
24
Questions?
CSE 373 25WI
25
That’s all!
CSE 373 25WI
26