Потік (Thread)
Найменша одиниця виконання в межах процесу, яка має власний стек та регістри, але ділить пам'ять з іншими потоками.
A thread is a component of a process. Multiple threads can exist within the same process, sharing resources such as memory, while executing independently. Threads are 'lightweight' because context switching between them is faster than between processes since it doesn't require switching the memory context.
graph LR
Center["Потік (Thread)"]:::main
Rel_assembly_language["assembly-language"]:::related -.-> Center
click Rel_assembly_language "/terms/assembly-language"
Rel_cpu_cache["cpu-cache"]:::related -.-> Center
click Rel_cpu_cache "/terms/cpu-cache"
classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
linkStyle default stroke:#4b5563,stroke-width:2px;
🧠 Перевірка знань
🧒 Простими словами
Уявіть, що процес — це кухня ресторану, а потоки — це кухарі. Вони працюють в одній кімнаті, користуються одними продуктами та обладнанням одночасно, але кожен виконує свою частину замовлення.
🤓 Expert Deep Dive
Реалізація потоків залежить від OS. Linux використовує clone() з прапорцем CLONE_VM. У Windows API є чітке розмежування між CreateProcess та CreateThread. POSIX Threads (pthreads) — це стандарт API для C/C++. Важливим нюансом є необхідність синхронізації (mutex, semaphore) для уникнення race conditions. У CPython існує GIL (Global Interpreter Lock), який обмежує паралельне виконання байт-коду.