Threads Flashcards
Thread
Execution Stream within a process, separate stack, program counter, thread control block
Thread vs Fork
Threads are faster and more lightweight, threads share address and OS state, reducing protection between them
Thread Control Block
Contains program counter, stack pointer, register state, and stack, execution state
Benefits of Threads
Can bypass IO blocking, shared memory, faster context switching(No virtual memory change)
Concurrency vs Parallelism
Concurrency interleaves program execution while parallelism runs instructions of separate cores
What do Threads Share
Address space(Global variables, heap, code) and OS State(files, sockets, locks)
Execution State
Whether a thread is New, Running, Blocked, Ready, or Finished
Interleaving
Context Switches cause threads to mix their instructions together with no guarantee one thread will execute an instruction before the other
Number of threads when program starts
One thread for main
pthread_join()
Blocks until passed in thread exits
Function to create thread
pthread_create()
User level threads
Threads managed by one thread without kernel being aware.
Advantages of User-Level Threads
User-level do not require going into kernel therefore are faster, can run on any OS.
Disadvantages of Kernel-Level Threads
Being contained on one kernel thread, any block on one user thread will block all threads. Cannot go to sleep without all threads going to sleep.
Pthreads
C library for multi-threaded programming.