同步操作将从 OpenHarmony/docs 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Threads are the minimum running units that compete for system resources. They can use or wait to use CPUs and use system resources such as memory. They run independently from one another.
Threads in each process of the OpenHarmony kernel run and are scheduled independently. The scheduling of threads in a process is not affected by threads in other processes.
Threads in the OpenHarmony kernel use the preemptive scheduling mechanism, either round-robin (RR) scheduling or First In First Out (FIFO) scheduling.
Threads in the OpenHarmony kernel are assigned 32 priorities, ranging from 0 (highest) to 31 (lowest).
A high-priority thread in a process can preempt the resources of a low-priority thread in this process. The low-priority thread can be scheduled only after the high-priority thread is blocked or terminated.
A thread may have the following states:
Init: The thread is being created.
Ready: The thread is in the ready list and waits for being scheduled by the CPU.
Running: The thread is running.
Blocked: The thread is blocked and suspended. The Blocked states include pending (blocked due to lock, event, or semaphore issues), suspended (active pending), delay (blocked due to delays), and pendtime (blocked by waiting timeout of locks, events, or semaphores).
Exit: The thread stops running and waits for the parent thread to reclaim its control block resources.
Figure 1 State transition of a thread
Description of the thread state transition:
Init→Ready:
When a thread is created, the thread enters the Init state to start initialization after obtaining the control block. After the thread is initialized, the thread is inserted into the scheduling queue and therefore enters the Ready state.
Ready→Running:
When a thread switchover is triggered, the thread with the highest priority in the ready list is executed and enters the Running state. This thread will be deleted from the ready list.
Running→Blocked:
When a running thread is blocked (for example, is pended, delayed, or reading semaphores), the thread is deleted from the ready list, and its state changes from Running to Blocked. Then, a thread switchover is triggered to run the thread with the highest priority in the ready list.
Blocked→Ready:
After the blocked thread is restored (the thread is restored, the delay times out, the semaphore reading times out, or the semaphore is read), the thread is added to the ready list and changes from the Blocked state to the Ready state.
Ready→Blocked:
A thread may also be blocked (suspended) in the Ready state. The blocked thread will change from the Ready state to the Blocked state and is deleted from the ready list. In this case, the thread will not be scheduled until it is restored.
Running→Ready:
After a thread with a higher priority is created or restored, threads will be scheduled. The thread with the highest priority in the ready list will change to the Running state. The originally running thread will change from the Running state to the Ready state and be added to the ready list.
Running→Exit:
When a running thread is terminated, its state changes from Running to Exit. The thread without the PTHREAD_CREATE_DETACHED attribute will present the Exit state after being terminated.
After a thread is created, it can be scheduled, suspended, restored, and delayed in user space. In addition, you can set and obtain the scheduling priority and scheduling policy of the thread.
The following table describes the APIs provided by the thread management module of the OpenHarmony kernel.
Table 1 APIs provided by the thread management module
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。