Thread Overview

박영재·2024년 10월 2일

Thread

OS allocates resources to process.

  • Process: management unit
  • Thread: execution unit included in a process

Context

  • Each thread has its own context
    • TLS
    • Stack
    • Register set4

Shared resource

  • Threads in a process share its resources
    • Heap, Data, File, etc…

→ 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

    1. InputThread pushes an event

    2. UIThread pops out

    3. UIThread creates a thread that will handle the event

    4. 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;
profile
People live above layers of abstraction beneath which engineers reside

0개의 댓글