Quick anatomy of a thread

Generally speaking, there are two different types of threads:

  • User-level threads: Threads that we can create and manage in order to perform a task
  • Kernel-level threads: Low-level threads that run in kernel mode and act on behalf of the operating system

Given that Python works at the user level, we're not going to deep dive into kernel threads at this time. Instead, we will explore several examples of user-level threads in this chapter's examples.

A thread can be in any of the following states:

  • New thread: A thread that hasn't started yet, and hasn't been allocated any resources.
  • Runnable: The thread is waiting to run. It has all the resources needed to run, and as soon as the scheduler gives it the green light, it will be run.
  • Running: A thread whose stream of instructions is being executed. From this state, it can go back to a non-running state, or die.
  • Not-running: A thread that has been paused. This could be due to another thread taking precedence over it, or simply because the thread is waiting for a long-running IO operation to finish.
  • Dead: A thread that has died because it has reached the natural end of its stream of execution, or it has been killed.

Transitions between states are provoked either by our actions or by the scheduler. There is one thing to bear in mind, though; it is best not to interfere with the death of a thread.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset