# Thread - [Basic Concepts](#section1179311337405) - [When to Use](#section44877547404) - [API Description](#section2069477134115) ## 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 **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\), 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. ## 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. ## API Description 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