Thread
OS allocates resources to process.

- Process: management unit
- Thread: execution unit included in a process
Context
- Each thread has its own context
Shared resource
- Threads in a process share its resources
→ Shared resources cause race condition
→ Synchronization is required
Overhead
- Context switching
- Synchronization → deadlock
- Cache miss
- Thread creation
- Scheduling
Synchronization
- Critical Section: Resource that more than one threads access

-
Synchronization Object
- User level - critical section
- Kernel level - mutex, semaphore
-
Deadlock
- Thread1 locks resource1 and tries to get resource2
- Thread2 locks resource2 and tries to get resource1
- Both Thread1 and Thread2 do nothing but to wait for each locked resource

Event-loop

-
InputThread pushes an event
-
UIThread pops out
-
UIThread creates a thread that will handle the event
-
UIThread can keep reacting to another event almost immediately
obj.compare_exchange(expected, desired);
if (obj == expected){
obj = desired;
return true;
}
expected = desired;
return false;