Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Thread (computing)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Related concepts == Scheduling can be done at the kernel level or user level, and multitasking can be done [[Preemption (computing)|preemptively]] or [[Cooperative multitasking|cooperatively]]. This yields a variety of related concepts. ===Processes=== {{Main|Process (computing)}} At the kernel level, a ''process'' contains one or more ''kernel threads'', which share the process's resources, such as memory and file handles β a process is a unit of resources, while a thread is a unit of scheduling and execution. Kernel scheduling is typically uniformly done preemptively or, less commonly, cooperatively. At the user level a process such as a [[runtime system]] can itself schedule multiple threads of execution. If these do not share data, as in Erlang, they are usually analogously called processes,<ref>{{Cite web |title=Erlang: 3.1 Processes |url=http://www.erlang.org/doc/getting_started/conc_prog.html}}</ref> while if they share data they are usually called ''(user) threads'', particularly if preemptively scheduled. Cooperatively scheduled user threads are known as ''[[fiber (computer science)|fibers]]''; different processes may schedule user threads differently. User threads may be executed by kernel threads in various ways (one-to-one, many-to-one, many-to-many). The term "[[light-weight process]]" variously refers to user threads or to kernel mechanisms for scheduling user threads onto kernel threads. A ''process'' is a "heavyweight" unit of kernel scheduling, as creating, destroying, and switching processes is relatively expensive. Processes own [[Resource (computer science)|resources]] allocated by the operating system. Resources include memory (for both code and data), [[Handle (computing)|file handles]], sockets, device handles, windows, and a [[process control block]]. Processes are ''isolated'' by [[process isolation]], and do not share address spaces or file resources except through explicit methods such as inheriting file handles or shared memory segments, or mapping the same file in a shared way β see [[interprocess communication]]. Creating or destroying a process is relatively expensive, as resources must be acquired or released. Processes are typically preemptively multitasked, and process switching is relatively expensive, beyond basic cost of [[context switching]], due to issues such as cache flushing (in particular, process switching changes virtual memory addressing, causing invalidation and thus flushing of an untagged [[translation lookaside buffer]] (TLB), notably on x86). ===Kernel threads=== {{anchor|kernel thread}} A ''kernel thread'' is a "lightweight" unit of kernel scheduling. At least one kernel thread exists within each process. If multiple kernel threads exist within a process, then they share the same memory and file resources. Kernel threads are preemptively multitasked if the operating system's process [[Scheduling (computing)|scheduler]] is preemptive. Kernel threads do not own resources except for a [[call stack|stack]], a copy of the [[processor register|registers]] including the [[program counter]], and [[thread-local storage]] (if any), and are thus relatively cheap to create and destroy. Thread switching is also relatively cheap: it requires a context switch (saving and restoring registers and stack pointer), but does not change virtual memory and is thus cache-friendly (leaving TLB valid). The kernel can assign one or more software threads to each core in a CPU (it being able to assign itself multiple software threads depending on its support for multithreading), and can swap out threads that get blocked. However, kernel threads take much longer than user threads to be swapped. ===User threads=== {{anchor|user thread}} Threads are sometimes implemented in [[User space|userspace]] libraries, thus called ''user threads''. The kernel is unaware of them, so they are managed and scheduled in userspace. Some implementations base their user threads on top of several kernel threads, to benefit from [[Multiprocessing|multi-processor]] machines ([[#Threading models|M:N model]]). User threads as implemented by [[virtual machine]]s are also called [[green threads]]. As user thread implementations are typically entirely in userspace, context switching between user threads within the same process is extremely efficient because it does not require any interaction with the kernel at all: a context switch can be performed by locally saving the CPU registers used by the currently executing user thread or fiber and then loading the registers required by the user thread or fiber to be executed. Since scheduling occurs in userspace, the scheduling policy can be more easily tailored to the requirements of the program's workload. However, the use of blocking system calls in user threads (as opposed to kernel threads) can be problematic. If a user thread or a fiber performs a system call that blocks, the other user threads and fibers in the process are unable to run until the system call returns. A typical example of this problem is when performing I/O: most programs are written to perform I/O synchronously. When an I/O operation is initiated, a system call is made, and does not return until the I/O operation has been completed. In the intervening period, the entire process is "blocked" by the kernel and cannot run, which starves other user threads and fibers in the same process from executing. A common solution to this problem (used, in particular, by many green threads implementations) is providing an I/O API that implements an interface that blocks the calling thread, rather than the entire process, by using non-blocking I/O internally, and scheduling another user thread or fiber while the I/O operation is in progress. Similar solutions can be provided for other blocking system calls. Alternatively, the program can be written to avoid the use of synchronous I/O or other blocking system calls (in particular, using non-blocking I/O, including lambda continuations and/or async/[[await]] primitives<ref>{{Cite AV media |first=Sergey |last=Ignatchenko |title=Eight Ways to Handle Non-blocking Returns in Message-passing Programs: from C++98 via C++11 to C++20 |url=https://www.youtube.com/watch?v=6lXUrvlMXNU |url-status=bot: unknown |archive-url=https://web.archive.org/web/20201125224240/https://www.youtube.com/watch?v=6lXUrvlMXNU&gl=US&hl=en |archive-date=2020-11-25 |publisher=CPPCON |access-date=2020-11-24 }}</ref>). ===Fibers=== {{Main|Fiber (computer science)}} [[Fiber (computer science)|Fibers]] are an even lighter unit of scheduling which are [[cooperative multitasking|cooperatively scheduled]]: a running fiber must explicitly "[[Yield (multithreading)|yield]]" to allow another fiber to run, which makes their implementation much easier than kernel or [[#User threads|user threads]]. A fiber can be scheduled to run in any thread in the same process. This permits applications to gain performance improvements by managing scheduling themselves, instead of relying on the kernel scheduler (which may not be tuned for the application). Some research implementations of the [[OpenMP]] parallel programming model implement their tasks through fibers.<ref>{{Cite conference |date=September 2022 |title=Enhancing MPI+OpenMP Task Based Applications for Heterogeneous Architectures with GPU support |url=https://hal-cea.archives-ouvertes.fr/cea-03749364/file/2022_iwomp_omp-target.pdf |conference=IWOMP 2022: 18th International Workshop on OpenMP |book-title=OpenMP in a Modern World: From Multi-device Support to Meta Programming. |series=Lecture Notes in Computer Science |doi=10.1007/978-3-031-15922-0_1 |last1=Ferat |first1=Manuel |last2=Pereira |first2=Romain |last3=Roussel |first3=Adrien |last4=Carribault |first4=Patrick |last5=Steffenel |first5=Luiz-Angelo |last6=Gautier |first6=Thierry |volume=13527 |pages=3β16 |isbn=978-3-031-15921-3 |s2cid=251692327 }}</ref><ref>{{Cite conference |title=BOLT: Optimizing OpenMP Parallel Regions with User-Level Threads |first1=Shintaro |last1=Iwasaki |first2=Abdelhalim |last2=Amer |first3=Kenjiro |last3=Taura |first4=Sangmin |last4=Seo |first5=Pavan |last5=Balaji |conference=The 28th International Conference on Parallel Architectures and Compilation Techniques |url=https://www.mcs.anl.gov/~iwasaki/pdfs/papers/PACT2019_paper.pdf}}</ref> Closely related to fibers are [[coroutine]]s, with the distinction being that coroutines are a language-level construct, while fibers are a system-level construct. ===Threads vs processes=== Threads differ from traditional [[computer multitasking|multitasking]] operating-system [[process (computing)|processes]] in several ways: * processes are typically independent, while threads exist as subsets of a process * processes carry considerably more [[state (computer science)|state]] information than threads, whereas multiple threads within a process share process state as well as [[computer storage|memory]] and other [[resource (computer science)|resources]] * processes have separate [[address space]]s, whereas threads share their address space * processes interact only through system-provided [[inter-process communication]] mechanisms * [[context switch]]ing between threads in the same process typically occurs faster than context switching between processes Systems such as [[Windows NT]] and [[OS/2]] are said to have ''cheap'' threads and ''expensive'' processes; in other operating systems there is not so great a difference except in the cost of an [[address space|address-space]] switch, which on some architectures (notably [[x86]]) results in a [[translation lookaside buffer]] (TLB) flush. Advantages and disadvantages of threads vs processes include: * ''Lower resource consumption'' of threads: using threads, an application can operate using fewer resources than it would need when using multiple processes. * ''Simplified sharing and communication'' of threads: unlike processes, which require a [[message passing]] or shared memory mechanism to perform [[inter-process communication]] (IPC), threads can communicate through data, code and files they already share. * ''Thread crashes a process'': due to threads sharing the same address space, an illegal operation performed by a thread can crash the entire process; therefore, one misbehaving thread can disrupt the processing of all the other threads in the application.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)