当前仅显示指定条件回帖 [ 展开查看全部 ]
文件名称:
thread.md
所在目录:
Openharmony v1.0 / docs / docs-en / kernel
文件大小:
59.59 KB
下载地址:
文本预览:
# Thread
## Basic Concepts
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 **pend** \(blocked due to lock, event, or semaphore issues\), **suspend** \(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/Blocked→Running:
After the blocked thread is restored \(for example, the thread is restored, the delay times out, the semaphore reading times out, or semaphores have been read\), the thread is added to the ready list and changes from the **Blocked** state to the **Ready** state. In this case, if the priority of the restored thread is higher than that of the running thread, a thread switchover occurs to run the restored thread, and therefore the restored thread changes from the **Ready** state to the **Running** 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.
- Blocked→Exit:
If an API is called to delete a blocked thread, the thread changes from the **Blocked** state to the **Exit** state.
## When to Use
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.
## Available APIs
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
## Basic Concepts
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 **pend** \(blocked due to lock, event, or semaphore issues\), **suspend** \(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/Blocked→Running:
After the blocked thread is restored \(for example, the thread is restored, the delay times out, the semaphore reading times out, or semaphores have been read\), the thread is added to the ready list and changes from the **Blocked** state to the **Ready** state. In this case, if the priority of the restored thread is higher than that of the running thread, a thread switchover occurs to run the restored thread, and therefore the restored thread changes from the **Ready** state to the **Running** 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.
- Blocked→Exit:
If an API is called to delete a blocked thread, the thread changes from the **Blocked** state to the **Exit** state.
## When to Use
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.
## Available APIs
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
点赞
回复
X