+ All Categories
Home > Documents > Professor: Shu-Ching Chen TA: Hsin-Yu Ha. An independent stream of instructions that can be...

Professor: Shu-Ching Chen TA: Hsin-Yu Ha. An independent stream of instructions that can be...

Date post: 16-Dec-2015
Category:
Upload: sarina-grinder
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
21
Multithreading Tutorial Professor: Shu-Ching Chen TA: Hsin-Yu Ha
Transcript
Page 1: 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;

MultithreadingTutorial

Professor: Shu-Ching ChenTA: Hsin-Yu Ha

Page 2: 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;

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;

Seq

uen

tially execute

int a, b;

a = 1;b = a + 2;

int c;

c = 5;

CPU

Multi-ThreadSingle-Thread

CPU

Page 3: 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;

Program v.s. Process v.s. Thread

Program 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

Page 4: 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;

Why is thread?

Parallel execution Shared resources Easier to create and destroy than

processes (100X) Easy porting to multiple CPUs

Page 5: 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;

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.

Page 6: 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;

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

Page 7: 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;

Creating threads (2)

Single variable

Page 8: 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;

Creating threads (3)

Several variables

Page 9: 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;

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

Page 10: 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;

Terminating Threads (2)

Example of pthread_exit()

Page 11: 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;

Terminating Threads (3)

pthread_join() Return 0 if OK, nonzero on error

Wait from other threads to terminate by calling it

Page 12: 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;

Terminating Threads (4)

Page 13: 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;

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

Page 14: 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;

Synchronizations

Join Mutexes Condition variables

Page 15: 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;

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

Page 16: 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;

Synchronization - Mutexes (2)

Page 17: 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;

Synchronization - Mutexes (3)

Page 18: 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;

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

Page 19: 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;

Synchronization – Condition Variables (2)

Page 20: 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;

References

POSIX Threads Programming https://computing.llnl.gov/tutorials/pthre

ads/

Pthreads primer http://pages.cs.wisc.edu/~

travitch/pthreads_primer.htmlPOSIX thread (pthread) Tutorial

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

Page 21: 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;

More tutorial and sample codecan be found…

Tutorial- Wiki http://en.wikibooks.org/wiki/C%2B%2B_Programming/Threading

Tutorial and sample code- C++ Multithreading http://www.tutorialspoint.com/cplusplus/cpp_multithreading.htm- Multithreading pthread http://www.bogotobogo.com/cplusplus/multithreading_pthread.php- Posix threads C http://codebase.eu/tutorial/posix-threads-c/ - Code project http://www.codeproject.com/Articles/14746/Multithreading-Tutorial

Tutorial Youtube Video- C++ Multithreading Pt. 1 http://youtu.be/o9ToXNdHANE- C++ Multithreading Pt. 2 http://youtu.be/a4mRwxWBJxA


Recommended