1 / 20

Multithreading Tutorial

Professor: Shu-Ching Chen TA: Hsin -Yu Ha. Multithreading Tutorial. Sequentially execute. What is a Thread?. A n independent stream of instructions that can be scheduled to run A path of execution. Multi-Thread. Single-Thread. int a, b; int c; a = 1; b = a + 2; c = 3;.

temple
Télécharger la présentation

Multithreading Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Professor: Shu-Ching Chen TA: Hsin-Yu Ha MultithreadingTutorial

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

  3. 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 Program Process Thread HDD Memory

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

  5. 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.

  6. 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

  7. Creating threads (2) • Single variable

  8. Creating threads (3) • Several variables

  9. 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

  10. Terminating Threads (2) • Example of pthread_exit()

  11. Terminating Threads (3) • pthread_join() Return 0 if OK, nonzero on error • Wait from other threads to terminate by calling it

  12. Terminating Threads (4)

  13. 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

  14. Synchronizations • Join • Mutexes • Condition variables

  15. 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

  16. Synchronization - Mutexes(2)

  17. Synchronization - Mutexes(3)

  18. 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

  19. Synchronization – Condition Variables (2)

  20. References • POSIX Threads Programming • 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

More Related