Download presentation
Presentation is loading. Please wait.
1
Multithreading Tutorial
Professor: Shu-Ching Chen TA: Hsin-Yu Ha
2
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
3
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
4
Why is thread? Parallel execution Shared resources
Easier to create and destroy than processes (100X) Easy porting to multiple CPUs
5
Compiling a pthreads program
Return Result :
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 is execute 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
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 Semaphore Mutexes Barrier
15
Synchronization - Semaphore (1)
A separate POSIX standard does define them Important functions sem_init : Initialize a new semaphore sem_destroy : deallocate an existing semaphore sem_wait : sem_post
16
Synchronization - Semaphore (2)
17
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
18
Synchronization - Mutexes (2)
19
Synchronization - Mutexes (3)
20
Synchronization – Barrier (1)
It is used when some parallel computations need to "meet up" at certain points before continuing. Pthreads extension includes barriers as synchronization objects (available in Single UNIX Specification) Enable by #define _XOPEN_SOURCE 600 at start of file Initialize a barrier for count threads int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrier attr_t *attr, int count); Each thread waits on a barrier by calling int pthread_barrier_wait(pthread_barrier_t *barrier); Destroy a barrier int pthread_barrier_destroy(pthread_barrier_t *barrier);
21
Synchronization – Barrier (2)
22
References POSIX Threads Programming Pthreads primer
Pthreads primer POSIX thread (pthread) Tutorial
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.