.. _ThreadsOverview:

.. raw:: html

   <script>ODSA.SETTINGS.DISP_MOD_COMP = true;ODSA.SETTINGS.MODULE_NAME = "ThreadsOverview";ODSA.SETTINGS.MODULE_LONG_NAME = "Concurrency with Multithreading";ODSA.SETTINGS.MODULE_CHAPTER = "Concurrency with Multithreading"; ODSA.SETTINGS.BUILD_DATE = "2021-06-01 12:51:47"; ODSA.SETTINGS.BUILD_CMAP = false;JSAV_OPTIONS['lang']='en';JSAV_EXERCISE_OPTIONS['code']='java';</script>


.. |--| unicode:: U+2013   .. en dash
.. |---| unicode:: U+2014  .. em dash, trimming surrounding whitespace
   :trim:


.. This file is part of the OpenCSF eTextbook project. It was
.. auto-generated by scripts from the OpenDSA eTextbook project.
.. See https://OpenCSF.org for more details. OpenCSF is distributed
.. under a Creative Commons Attribution-NonCommercial 4.0 International
.. License (see http://creativecommons.org/licenses/by-nc/4.0/),
.. Copyright (c) 2019-2021 by Michael S. Kirkpatrick. OpenDSA is
.. distributed under an MIT open source license, Copyright (c) 2012-2021
.. by the OpenDSA Project Contributors.

.. avmetadata::
   :author: Michael S. Kirkpatrick
   :requires:
   :satisfies: Multithreading Introduction
   :topic: Multithreading

Concurrency with Multithreading
===============================

.. figure:: Images/CSF-Timeline.6.png
   :align: center
   :width: 90%
   :figwidth: 100%
   :alt: Timeline of major CSF topics with Multicore and Threads highlighted

.. raw:: html

    <blockquote class="blockquote text-center">
        <p>“The good news about computers is that they do what you tell them
        to do. The bad news is that they do what you tell them to do.”
        <footer class="blockquote-footer">Ted Nelson</footer>
    </blockquote>

Plus ça change, plus c'est la même chose. Or put another way,
everything old is new again. IBM OS/360 introduced threads in 1967. The 1990s
brought renewed interest in threads, with POSIX.1c-1995 standardizing their
interface. One key factor behind this interest was the development of Linux as
an open-source platform for cluster systems. Another factor was the impact of
the “power wall” on Moore's law and integrated circuit design. As a result of
this resurgence, threads are now unavoidable as a core abstraction in modern
computing.

.. topic:: Chapter Objectives

   .. figure:: Images/CSF-Images-Objectives.png
      :align: left
      :figwidth: 5%
      :width: 100%
      :alt: Decorative chapter objectives image

   In this chapter, we will address the following instructional objectives:

   * We will compare and contrast the concepts of threads and processes,
     describing the advantages and limitations of each.
   * We will examine code using the POSIX thread library, a library that is widely
     available on a variety of platforms.
   * We will explore race conditions as a new source of bugs that arise in
     multithreaded software.
   * We will consider the benefits and drawbacks of using implicit threading
     libraries and built-in language support.
     

Throughout the first part of this book, we referred to processes whenever we
were discussing a unit of execution. Specifically, in
`Processes and Multiprogramming <Multiprogramming.html>`_, we described
multiprogramming as the technique of dividing time on the CPU among multiple
processes. After one process ran for a certain amount of time, the kernel would
change the virtual address space to switch the execution context to a different
process.

While process context switching provides a robust mechanism for
multiprogramming, modern systems require a more efficient approach.
Specifically, many programmers write applications or systems programs that
achieve concurrency by switching between multiple :term:`threads of execution <thread>`
(or, more simply, :term:`threads <thread>`) within a single process. This technique is
known as :term:`multithreading`.

There are many different languages and software libraries available for writing
multithreaded code. Some of these options are unique to a single operating
system or programming language, while others are more broadly supported. In this
and later chapters, we will primarily focus on using the POSIX library (called
:term:`pthreads`), a C library available on all major platforms, though we will
also explore some of the benefits of alternative approaches.


