Thread-Based Parallelism

Currently, the most widely used programming paradigm for the management of concurrency in software applications is based on multithreading. Generally, an application is made by a single process that is divided into multiple independent threads, which represent activities of different types that run in parallel and compete with each other.

Nowadays, modern applications that use multithreading have been adopted on a massive scale. In fact, all current processors are multicore, just so they can perform parallel operations and exploit the computer's computational resources.

Hence, multithreaded programming is definitely a good way to achieve concurrent applications. However, multithreaded programming often hides some non-trivial difficulties, which must be managed appropriately to avoid errors such as deadlocks or synchronization issues.

We will first define the concepts of thread-based and multithreaded programming and then introduce the multithreading library. We will learn about the main directives for thread definition, management, and communication.

Through the multithreading library, we will see how to solve problems through different techniques, such as lock, RLock, semaphores, conditioneventbarrier, and queue.

In this chapter, we will cover the following recipes:

  • What is a thread?
  • How to define a thread
  • How to determine the current thread
  • How to use a thread in a subclass
  • Thread synchronization with a lock
  • Thread synchronization with an RLock

  • Thread synchronization with semaphores
  • Thread synchronization with a condition
  • Thread synchronization with an event
  • Thread synchronization with a barrier
  • Thread communication using a queue

We will also explore the main options offered by Python to program with threads. To do this, we will focus on using the threading module.

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

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