Multithreading Tutorial

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
Professor: Shu-Ching Chen TA: Hsin-Yu Ha.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int c;
PTHREADS These notes are from LLNL Pthreads Tutorial
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
The University of Adelaide, School of Computer Science
Today’s topic Pthread Some materials and figures are obtained from the POSIX threads Programming tutorial at
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
POSIX Threads Programming Operating Systems. Processes and Threads In shared memory multiprocessor architectures, such as SMPs, threads can be used to.
Professor: Shu-Ching Chen TA: Samira Pouyanfar.  An independent stream of instructions that can be scheduled to run  A path of execution int a, b; int.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Pthreads.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.
Threads A thread is an alternative model of program execution
NCHU System & Network Lab Lab #6 Thread Management Operating System Lab.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
Thread Programming 김 도 형김 도 형. 2 Table of Contents  What is a Threads?  Designing Threaded Programs  Synchronizing Threads  Managing Threads.
B. RAMAMURTHY 5/10/2013 Amrita-UB-MSES Realizing Concurrency using the thread model.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Tutorial 4. In this tutorial session we’ll see Threads.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Realizing Concurrency using the thread model
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Realizing Concurrency using the thread model
Principles of Operating Systems Lecture 11
Threads in C Caryl Rahn.
Shared-Memory Programming with Threads
Threads Threads.
Boost String API & Threads
CS399 New Beginnings Jonathan Walpole.
Thread Programming.
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Principles of Operating Systems Lecture 8
Multithreading Tutorial
Realizing Concurrency using Posix Threads (pthreads)
Programming with Shared Memory
CSE 333 – Section 9 Threads.
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Thread Programming.
Realizing Concurrency using the thread model
CS510 Operating System Foundations
Jonathan Walpole Computer Science Portland State University
Unix System Calls and Posix Threads
Pthread Prof. Ikjun Yeom TA – Mugyo
Operating System Concepts
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Tutorial 4.
CS333 Intro to Operating Systems
CS 144 Advanced C++ Programming May 7 Class Meeting
Shared Memory Programming with Pthreads
POSIX Threads(pthreads)
Presentation transcript:

Multithreading Tutorial Professor: Shu-Ching Chen TA: Yimin Yang

What is a Thread? An independent stream of instructions that can be scheduled to run A path of execution int a, b; int c; a = 1; b = a + 2; c = 3; Sequentially execute c = 5; CPU Multi-Thread Single-Thread

Program v.s. Process v.s. Thread An execution file stored in the harddrive Process An execution file stored in the Memory Thread An execution path of part of the process HDD Memory Program Process Thread

Why is thread? Parallel execution Shared resources Easier to create and destroy than processes (100X) Easy porting to multiple CPUs

The Pthread API Standardized C language threads programming interface for UNIX systems Four major groups Thread management: Routines that work directly on threads - creating, detaching, joining, etc. Mutex: Routines that deal with synchronization. Mutex functions provide for creating, destroying, locking and unlocking mutexes. Condition variable: Routines that address communications between threads that share a mutex. Synchronization: Routines that manage read/write locks and barriers.  For UNIX systems, a standardized C language threads programming interface has been specified by the IEEE POSIX 1003.1c standard. Implementations that adhere to this standard are referred to as POSIX threads, or Pthreads. Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library - though this library may be part of another library, such as libc, in some implementations. Thread management: Routines that work directly on threads - creating, detaching, joining, etc. They also include functions to set/query thread attributes (joinable, scheduling etc.) Mutexes: Routines that deal with synchronization, called a "mutex", which is an abbreviation for "mutual exclusion". Mutex functions provide for creating, destroying, locking and unlocking mutexes. These are supplemented by mutex attribute functions that set or modify attributes associated with mutexes. Condition variables: Routines that address communications between threads that share a mutex. Based upon programmer specified conditions. This group includes functions to create, destroy, wait and signal based upon specified variable values. Functions to set/query condition variable attributes are also included. Synchronization: Routines that manage read/write locks and barriers.

Creating threads (1) pthread_create() Return 0 if OK, nonzero on error Four Arguments Thread : A thread identifier Attr : A pointer to a thread attribute object Start_routine : A pointer to the function the thread executes Arg : The argument to the function By default, a thread is created with certain attributes. Some of these attributes can be changed by the programmer via the thread attribute object. pthread_attr_init and pthread_attr_destroy are used to initialize/destroy the thread attribute object. Other routines are then used to query/set specific attributes in the thread attribute object. Attributes include: Detached or joinable state Scheduling inheritance Scheduling policy Scheduling parameters Scheduling contention scope Stack size Stack address Stack guard (overflow) size

Creating threads (2) Single variable

Creating threads (3) Several variables

Terminating Threads (1) pthread_exit() Return 0 if OK, nonzero on error Four ways of terminating a thread The thread returns from its starting routine The thread makes a call to the pthread_exit subroutine The thread is canceled by another thread The entire process is terminated If main() finishes first, without calling pthread_exit

Terminating Threads (2) Example of pthread_exit()

Terminating Threads (3) pthread_join() Return 0 if OK, nonzero on error Wait from other threads to terminate by calling it "Joining" is one way to accomplish synchronization between threads. The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates. The programmer is able to obtain the target thread's termination return status if it was specified in the target thread's call to pthread_exit(). A joining thread can match one pthread_join() call. It is a logical error to attempt multiple joins on the same thread.

Terminating Threads (4) "Joining" is one way to accomplish synchronization between threads.

Other Thread functions pthread_self() It returns the unique, system assigned thread ID of the calling thread pthread_detach() Return 0 if OK, nonzero on error It can be used to explicitly detach a thread

Synchronizations Join Mutexes Condition variables A join is performed when one wants to wait for a thread to finish. A thread calling routine may launch multiple threads then wait for them to finish to get the results. One waits for the completion of the threads with a join. mutex variables - mutual exclusion condition variables - to name events of interest

Synchronization - Mutexes (1) Mutexes are used to prevent data inconsistencies due to operations by multiple threads upon the same memory area performed at the same time to prevent race conditions where an order of operation upon the memory is expected mutexes has to do with thread synchronization (simultaneous running threads) and the protection of critical code segments (shared data). Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables.

Synchronization - Mutexes (2) If register load and store operations for the incrementing of variable counter occurs with unfortunate timing, it is theoretically possible to have each thread increment and overwrite the same variable with the same value. Another possibility is that thread two would first increment counter locking out thread one until complete and then thread one would increment it to 2. 

Synchronization - Mutexes (3)

Synchronization – Condition Variables (1) Condition variables are used to allow threads to synchronize based upon the actual value of data without continually polling to check whether the condition if met in conjunction with a mutex lock Condition variables provide yet another way for threads to synchronize. While mutexes implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data. A CV may come very useful for waking up a sleeping (blocked) thread, e.g. signaling data availability etc. as a tool for simple inter-thread communication. This instead of e.g. using shared memory if the destination thread is supposed to operate short-term and then go back to sleep. Waiting on a CV is a blocking operation so the thread would only consume CPU when there is actually data available to operate on.

Synchronization – Condition Variables (2)

References POSIX Threads Programming Pthreads primer https://computing.llnl.gov/tutorials/pthreads/ Pthreads primer http://pages.cs.wisc.edu/~travitch/pthreads_primer.html POSIX thread (pthread) Tutorial http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html