Institute for Advanced Professional Studies

Linux Multithreaded Programming: Servers and Applications

Overview of Linux Multithreaded Programming:

Many applications juggle multiple chores at once. Server applications handle concurrent clients; interactive applications often deal with user input while processing background computations; computationally intensive applications harness the power of multiple processors. The common theme is the use of multiple threads of control to provide contexts for dealing with concurrent activities, whether multiplexed on one processor, executed in parallel on multiple processors, or taking advantage of the facilities of processors with Hyper-Threading Technology and new dual-core processors from AMD and Intel.

Coordinating the execution of such threads involves synchronizing access to shared data structures, insuring that programs are well behaved and deterministic regardless of the relative speeds of execution of their component threads. Multithreaded programs, like single-threaded programs, must deal with exceptions and interaction with the outside world. Though there may be many concurrent activities in such a program, the program as a whole should cleanly respond to such outside input.

There are a number of ways in which threads have been implemented, involving user-level libraries, the kernel, and various combinations. Most Linux implementations currently treat each thread as a separate process created using the clone system call (though each shares its address space with its cohorts); newer implementations of threads on Linux use different approaches; other versions of UNIX use still different approaches. We discuss them all, along with new features of POSIX threads. (Note that threads are sometimes known as lightweight processes, though this term takes on a particular, but related, technical meaning in Solaris.)

Linux Workshop Overview:

This hands-on course covers Linux multithreaded programming using the POSIX 1003.1c interface. A particular emphasis of the course is the design of high-performance servers. We address uniprocessors and multiprocessors, as well as Intel's hyper-threaded processors. We cover the use of POSIX Threads (also known as pthreads) not only in C programs, but also in C++ programs.

The course has two major themes: Writing reliable, maintainable programs and writing high-performing programs. An important sub theme of the course is determining when it is appropriate to use multithreaded programming techniques. Much of the course is spent learning and practicing how to write, test and enhance multithreaded programs, covering common pitfalls, and working with numerous examples.

50% of this course is devoted to guided, hands-on programming exercises, ensuring that participants gain sufficient experience with multithreaded program design, coding, error detection and debugging techniques. Among the areas emphasized in the course are the special concerns for programming multiprocessors, particularly techniques for taking maximum advantage of the available processors. Participants learn how threads packages are implemented so that they can understand the performance implications of the various approaches when writing multithreaded programs.

A major module focuses on the design of scalable servers. Included here are techniques for improving responsiveness to network requests and for reducing overhead in handling such requests.

IAPS can customize Linux training to achieve specific organizational objectives. We also offer multithreaded programming training for Windows (Win32), Solaris and UNIX (POSIX) for groups developing and supporting embedded real-time systems, as well as desktop, client and server applications on uniprocessor and multiprocessor platforms.

Linux Workshop Objectives:

Upon successful completion of this course, participants will be able to:

  • Define threads and explain how they are different from processes.
  • Describe what sorts of applications should be written as multithreaded applications.
  • Describe what sorts of applications should not be written as multithreaded applications.
  • Design, write, test and debug multithreaded applications for Linux systems.
  • Describe the difference between parallelism and concurrency and show how to take advantage of each with threads.
  • Explain what Intel's Hyper-Threading Technology is and describe how multithreaded programs can take advantage of it.
  • Describe the architecture and key features of the latest dual-core processors and explain how multithreaded programs should be designed to harness their computing power.
  • Utilize POSIX standards to write portable multithreaded programs.
  • Describe synchronization and how it applies to multithreaded programs.
  • Use mutexes, condition variables, and semaphores on Linux and other UNIX systems.
  • Describe what a barrier is and demonstrate how to implement it using POSIX threads.
  • Use assertions and simple verification techniques to demonstrate that a multithreaded program is correct.
  • Explain why some libraries cannot be easily used with multithreaded programs (i.e., are not "thread-safe") and describe the subtle and not-so-subtle errors that could occur when using them.
  • Demonstrate the use of thread specific data and other techniques for dealing with these problems.
  • Use threads to build high-performance server applications.
  • Describe how asynchrony enters into multithreaded programs.
  • Use signals and cancels within multithreaded programs on Linux systems.
  • Describe the trade-offs between using threads and processes.
  • Coordinate the threads of different processes.
  • Describe how the architecture of multiprocessor computers affects the design of multithreaded programs.
  • Describe the strategies employed in a number of threads implementations and how they affect performance and scheduling.

Topic Outline:

  • Introduction
    • What is concurrency?
    • What is parallelism?
    • What is hyper-threading and on which processors is it available?
    • Why should one write multithreaded programs?
    • Why aren't processes good enough?
    • POSIX 1003.1c: The UNIX threads API
    • Win32 threads: another implementation approach
  • Adding Threads to C Programs
    • Creating and terminating threads
    • Passing arguments to threads
    • C examples
    • Special concerns for object-oriented programs
    • C++ examples
    • Hands-on exercises
  • Synchronization
    • Mutual exclusion with mutexes
    • Condition Variables
    • Semaphores
    • Barriers
    • How best to use the available constructs
    • C and C++ examples
      • Using assertions and simple verification techniques
    • Hands-on exercises
  • Deviations
    • Cancels and cancellation
    • Exception handling
    • UNIX Signals
    • Asynchronous signal safety
    • Hands-on exercises
  • Thread-Safe Libraries
    • Thread safety issues
    • Thread-specific data: Global, but private, data
    • Handling shared storage
    • Examination of example libraries
    • Hands-on exercises
  • Threads and Processes
    • Coordinating the threads of different processes: cross-process synchronization
    • Hands-on exercises
  • Threads and Multiprocessors
    • Problems with the memory model
    • Utilizing multiprocessors effectively
    • Utilizing dual-core and hyper-threaded processors effectively
    • Testing, debugging, and enhancing the reliability and performance of multithreaded programs
    • Hands-on exercises (if a multiprocessor or hyper-threaded processor is available)
  • Designing High-performing Servers
    • Asynchronous vs. Synchronous I/O
    • Designing high-performance servers
    • Designing scalable servers
    • Hands-on exercises
  • Linux Threads Implementation and Scheduling Issues
    • The implementation of thread support
      • One-to-one model
      • Variable-weight processes
      • Many-to-one model
      • Many-to-many model
      • Scheduler activations
    • Interaction with OS scheduling

Course Duration:

3 days

Intended Audience:

Experienced C and C++ programmers who wish to become competent at writing multithreaded programs, such as high-performing high-reliability servers, on Linux platforms

Technical Prerequisites:

Participants should be experienced C and C++ programmers: They must be comfortable with all the major features of the languages, including pointers, dynamic storage allocation, classes, abstract types, and inheritance. They should have experience programming on Linux (or other UNIX systems) and should know how to use standard facilities to edit, compile, and debug applications.

Course Format:

Hands-on lecture/workshop

© Copyright 1997-2015 Institute for Advanced Professional Studies (IAPS)