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!
==Scheduling== ===Preemptive vs cooperative scheduling=== Operating systems schedule threads either [[Preemption (computing)|preemptively]] or [[Cooperative multitasking|cooperatively]]. [[Operating system#Single- and multi-user| Multi-user operating systems]] generally favor [[preemptive multithreading]] for its finer-grained control over execution time via [[context switch]]ing. However, preemptive scheduling may context-switch threads at moments unanticipated by programmers, thus causing [[lock convoy]], [[priority inversion]], or other side-effects. In contrast, [[cooperative multithreading]] relies on threads to relinquish control of execution, thus ensuring that threads [[Run to completion scheduling |run to completion]]. This can cause problems if a cooperatively multitasked thread [[Blocking (computing) |blocks]] by waiting on a [[Resource (computer science)| resource]] or if it [[Starvation (computer science) |starves]] other threads by not yielding control of execution during intensive computation. === Single- vs multi-processor systems === Until the early 2000s, most desktop computers had only one single-core CPU, with no support for [[hardware thread]]s, although threads were still used on such computers because switching between threads was generally still quicker than full-process [[context switch]]es. In 2002, [[Intel]] added support for [[simultaneous multithreading]] to the [[Pentium 4]] processor, under the name ''[[hyper-threading]]''; in 2005, they introduced the dual-core [[Pentium D]] processor and [[AMD]] introduced the dual-core [[Athlon 64 X2]] processor. Systems with a single processor generally implement multithreading by [[Preemption (computing)#Time slice|time slicing]]: the [[central processing unit]] (CPU) switches between different ''software threads''. This [[context switch]]ing usually occurs frequently enough that users perceive the threads or tasks as running in parallel (for popular server/desktop operating systems, maximum time slice of a thread, when other threads are waiting, is often limited to 100β200ms). On a [[multiprocessor]] or [[multi-core]] system, multiple threads can execute in [[parallel computing|parallel]], with every processor or core executing a separate thread simultaneously; on a processor or core with ''[[hardware thread]]s'', separate software threads can also be executed concurrently by separate hardware threads. ===Threading models=== ====1:1 (kernel-level threading)==== Threads created by the user in a 1:1 correspondence with schedulable entities in the kernel<ref name="OSConcepts">{{Cite book |first1=Abraham |last1=Silberschatz |author-link1=Abraham Silberschatz |first2=Peter Baer |last2=Galvin |first3=Greg |last3=Gagne |title=Operating system concepts |publisher=Wiley |year=2013 |isbn=9781118063330 |edition=9th |location=Hoboken, N.J. |pages=170β171}}</ref> are the simplest possible threading implementation. [[OS/2]] and [[Win32]] used this approach from the start, while on [[Linux]] the [[GNU C Library]] implements this approach (via the [[Native POSIX Thread Library|NPTL]] or older [[LinuxThreads]]). This approach is also used by [[Solaris (operating system)|Solaris]], [[NetBSD]], [[FreeBSD]], [[macOS]], and [[iOS]]. ====''M'':1 (user-level threading)==== An ''M'':1 model implies that all application-level threads map to one kernel-level scheduled entity;<ref name="OSConcepts" /> the kernel has no knowledge of the application threads. With this approach, context switching can be done very quickly and, in addition, it can be implemented even on simple kernels which do not support threading. One of the major drawbacks, however, is that it cannot benefit from the hardware acceleration on [[Multithreading (computer hardware)|multithreaded]] processors or [[Multiprocessing|multi-processor]] computers: there is never more than one thread being scheduled at the same time.<ref name="OSConcepts" /> For example: If one of the threads needs to execute an I/O request, the whole process is blocked and the threading advantage cannot be used. The [[GNU Portable Threads]] uses User-level threading, as does [[State Threads]]. ====''M'':''N'' (hybrid threading)==== ''M'':''N'' maps some {{var|M}} number of application threads onto some {{var|N}} number of kernel entities,<ref name="OSConcepts" /> or "virtual processors." This is a compromise between kernel-level ("1:1") and user-level ("''N'':1") threading. In general, "''M'':''N''" threading systems are more complex to implement than either kernel or user threads, because changes to both kernel and user-space code are required{{clarify|reason=no kernel modifications are needed|date=June 2020}}. In the M:N implementation, the threading library is responsible for scheduling user threads on the available schedulable entities; this makes context switching of threads very fast, as it avoids system calls. However, this increases complexity and the likelihood of [[priority inversion]], as well as suboptimal scheduling without extensive (and expensive) coordination between the userland scheduler and the kernel scheduler. ====Hybrid implementation examples==== * [[Scheduler activations]] used by older versions of the NetBSD native POSIX threads library implementation (an ''M'':''N'' model as opposed to a 1:1 kernel or userspace implementation model) * [[Light-weight process]]es used by older versions of the [[Solaris (operating system)|Solaris]] operating system * Marcel from the [[PM2]] project. * The OS for the Tera-[[Cray MTA-2]] * The [[Glasgow Haskell Compiler]] (GHC) for the language [[Haskell (programming language)|Haskell]] uses lightweight threads which are scheduled on operating system threads. ====History of threading models in Unix systems==== [[SunOS]] 4.x implemented ''[[light-weight process]]es'' or LWPs. [[NetBSD]] 2.x+, and [[DragonFly BSD]] implement LWPs as kernel threads (1:1 model). SunOS 5.2 through SunOS 5.8 as well as NetBSD 2 to NetBSD 4 implemented a two level model, multiplexing one or more user level threads on each kernel thread (M:N model). SunOS 5.9 and later, as well as NetBSD 5 eliminated user threads support, returning to a 1:1 model.<ref>{{Cite web |year=2002 |title=Multithreading in the Solaris Operating Environment |url=http://www.sun.com/software/whitepapers/solaris9/multithread.pdf |archive-url=https://web.archive.org/web/20090226174929/http://www.sun.com/software/whitepapers/solaris9/multithread.pdf |archive-date=February 26, 2009|url-status=dead}}</ref> FreeBSD 5 implemented M:N model. FreeBSD 6 supported both 1:1 and M:N, users could choose which one should be used with a given program using /etc/libmap.conf. Starting with FreeBSD 7, the 1:1 became the default. FreeBSD 8 no longer supports the M:N model.
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)