📄 threads.html
字号:
A hybrid model offers the speed of library threads and the concurrencyof kernel threads.In hybrid models, a process has some (relatively small) number ofkernel scheduled entities associated with it.It also has a potentially much larger number of library threadsassociated with it.Some library threads may be bound to kernel scheduled entities, whilethe other library threads are multiplexed onto the remaining kernelscheduled entities.There are two levels of thread scheduling:<ul><p><li>The run-time library manages the scheduling of (unbound) library threadsonto kernel scheduled entities.<p><li>The kernel manages the scheduling of kernel scheduled entitiesonto processors.<p></ul><p>For this reason, a hybrid model is referred to as a "two-levelthreads scheduling model".In this model, the process can have multiple concurrently executingthreads; specifically, it can have as many concurrently executingthreads as it has kernel scheduled entities.<h4><a name = "tag_000_010_004"> </a>Thread Mutexes</h4><p>A thread that has blocked will not prevent any unblocked threadthat is eligible to use the same processing resources fromeventually making forward progress in its execution.Eligibility for processing resources isdetermined by the scheduling policy.<p>A thread becomes the owner of a mutex,<i>m</i>,when either:<ol><p><li>it returns successfully from<i><a href="pthread_mutex_lock.html">pthread_mutex_lock()</a></i>with<i>m</i>as the<i>mutex</i>argument, or<p><li>it returns successfully from<i><a href="pthread_mutex_trylock.html">pthread_mutex_trylock()</a></i>with<i>m</i>as the<i>mutex</i>argument, or<p><li>it returns (successfully or not) from<i><a href="pthread_cond_wait.html">pthread_cond_wait()</a></i>with<i>m</i>as the<i>mutex</i>argument (except as explicitly indicated otherwise for certainerrors), or<p><li>it returns (successfully or not) from<i><a href="pthread_cond_timedwait.html">pthread_cond_timedwait()</a></i>with<i>m</i>as the<i>mutex</i>argument (except as explicitly indicated otherwise for certainerrors).<p></ol><p>The thread remains the owner of<i>m</i>until it either:<ol><p><li>executes<i><a href="pthread_mutex_unlock.html">pthread_mutex_unlock()</a></i>with<i>m</i>as the<i>mutex</i>argument, or<p><li>blocks in a call to<i><a href="pthread_cond_wait.html">pthread_cond_wait()</a></i>with<i>m</i>as the<i>mutex</i>argument, or<p><li>blocks in a call to<i><a href="pthread_cond_timedwait.html">pthread_cond_timedwait()</a></i>with<i>m</i>as the<i>mutex</i>argument.<p></ol><p>The implementation behaves as ifat all times there is at most oneowner of any mutex.<p>A thread that becomes theowner of a mutex is said to have<i>acquired</i>the mutex and the mutex is said to have become<i>locked</i>;when a thread gives up ownershipof a mutex it is said to have<i>released</i>the mutex and the mutex is said to have become<i>unlocked</i>.<h4><a name = "tag_000_010_005"> </a>Thread Scheduling Attributes</h4><p>In support of the scheduling interface, threads have attributeswhich are accessed through the<i>pthread_attr_t</i>thread creation attributes object.<p>The<i>contentionscope</i>attribute defines thescheduling contention scopeof the thread to be eitherPTHREAD_SCOPE_PROCESS or PTHREAD_SCOPE_SYSTEM .<p>The<i>inheritsched</i>attribute specifies whether a newly created thread isto inherit the scheduling attributes of the creating threador to have its scheduling values setaccording to the other scheduling attributes in the<i>pthread_attr_t</i>object.<p>The<i>schedpolicy</i>attribute defines the scheduling policy for the thread.The<i>schedparam</i>attribute defines the scheduling parameters for the thread.The interaction of threads having different policies within a processis described as part of the definition of those policies.<p>If the_POSIX_THREAD_PRIORITY_SCHEDULINGoption is defined, and the<i>schedpolicy</i>attribute specifies one of the priority-based policies definedunder this option, the<i>schedparam</i>attribute contains the scheduling priority of the thread.A conforming implementation ensures that the priority value in<i>schedparam</i>is in the range associated with the scheduling policywhen the thread attributes object is used to create a thread, orwhen the scheduling attributes of a thread are dynamically modified.The meaning of the priority value in<i>schedparam</i>is the same as that of<i>priority</i>.<p>When a process is created,its single thread has a scheduling policy and associated attributesequal to the process's policy and attributes.The defaultscheduling contention scopevalue isimplementation-dependent.The default values of other scheduling attributes areimplementation-dependent.<h4><a name = "tag_000_010_006"> </a>Thread Scheduling Contention Scope</h4><p>The scheduling contention scopeof a thread defines the set of threads with whichthe thread must compete for use of the processing resources.The scheduling operation will select at most one thread to executeon each processor at any point in timeand the thread's scheduling attributes (for example, priority),whether under process scheduling contention scopeor system scheduling contention scope,are the parameters used to determine the scheduling decision.<p>The scheduling contention scope,in the context of scheduling a mixed scope environment,effects threads as follows:<ul><p><li>A thread created withPTHREAD_SCOPE_SYSTEMscheduling contention scopecontends for resources with all other threads in the samescheduling allocation domainrelative to their system scheduling attributes.The system scheduling attributes of a thread created withPTHREAD_SCOPE_SYSTEM scheduling contention scopeare the scheduling attributes with which the thread was created.The system scheduling attributes of a thread created withPTHREAD_SCOPE_PROCESS scheduling contention scopeare the implementation-dependent mapping into system attribute spaceof the scheduling attributes with which the thread was created.<p><li>Threads created withPTHREAD_SCOPE_PROCESSscheduling contention scopecontend directly with other threads within their processthat were created withPTHREAD_SCOPE_PROCESSscheduling contention scope.The contention is resolved based onthe threads' scheduling attributes and policies.It is unspecified how such threads are scheduled relative tothreads in other processes or threads withPTHREAD_SCOPE_SYSTEMscheduling contention scope.<p><li>Conforming implementations supportthe PTHREAD_SCOPE_PROCESS scheduling contention scope,the PTHREAD_SCOPE_SYSTEM scheduling contention scope, or both.<p></ul><h4><a name = "tag_000_010_007"> </a>Scheduling Allocation Domain</h4><p>Implementations supportscheduling allocation domainscontaining one or more processors.It should be noted that the presence of multiple processors does notautomatically indicate ascheduling allocation domainsize greater than one.Conforming implementations on multi-processors may mapall or any subset of the CPUs to one or multiplescheduling allocation domains,and could define thesescheduling allocation domainson a per-thread,per-process, or per-system basis, depending on the types of applicationsintended to be supported by the implementation.The scheduling allocation domain is independent ofscheduling contention scope, as the scheduling contention scopemerely defines the set of threads with which a thread must contendfor processor resources, whilescheduling allocation domaindefines the set of processors for which it contends.The semantics of how this contention is resolved among threads for processorsis determined by the scheduling policies of the threads.<p>The choice of scheduling allocation domainsize and the level of application control overscheduling allocation domains is implementation-dependent.Conforming implementations may change the size ofscheduling allocation domains and the binding of threads toscheduling allocation domains at any time.<p>For application threads with scheduling allocation domainsof size equal to one, the scheduling rules defined forSCHED_FIFO and SCHED_RR will be used.All threads with system scheduling contention scope,regardless of the processes in which they reside,compete for the processor according to their priorities.Threads with process scheduling contention scopecompete only with other threads with processscheduling contention scope within their process.<p>For application threads with scheduling allocation domainsof size greater than one, the rules defined for SCHED_FIFOand SCHED_RR are used in an implementation-dependent manner.Each thread with system scheduling contention scopecompetes for the processors in its scheduling allocation domainin an implementation-dependent manner according to its priority.Threads with process scheduling contention scopeare scheduled relative to other threads within the samescheduling contention scope in the process.<h4><a name = "tag_000_010_008"> </a>Thread Cancellation</h4><p>The thread cancellation mechanism allows a thread to terminatethe execution of any other thread in the process in a controlled manner.The target thread (that is, the one that is being canceled) is allowedto hold cancellation requests pending in a number of ways andto perform application-specific cleanup processingwhen the notice of cancellation is acted upon.<p>Cancellation is controlled by the cancellation control interfaces.Each thread maintains its own cancelability state.Cancellation may only occur at cancellation pointsor when the thread is asynchronously cancelable.<p>The thread cancellation mechanism described in this sectiondepends upon programs having set<i>deferred cancelability</i>state, which is specified as the default.Applications must also carefully follow static lexical scoping rulesin their execution behaviour.For instance, use of<i><a href="setjmp.html">setjmp()</a></i>,return, goto, and so on, to leave user-defined cancellationscopes without doing the necessary scope pop operationwill result in undefined behaviour.<p>Use of asynchronous cancelability while holding resourceswhich potentially need to be released may result in resource loss.Similarly, cancellation scopes may only be safely manipulated(pushed and popped)when the thread is in the<i>deferred</i>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -