Professional Documents
Culture Documents
Practical Programming in C - LECTURE 12-6-087-January-Iap-2010 MIT6 - 087IAP10 - Lec12
Practical Programming in C - LECTURE 12-6-087-January-Iap-2010 MIT6 - 087IAP10 - Lec12
Practical Programming in C - LECTURE 12-6-087-January-Iap-2010 MIT6 - 087IAP10 - Lec12
Review
Multithreaded programming
Concepts
Pthread
API
Mutex
Condition variables
Review
Multithreaded programming
Concepts
Pthread
API
Mutex
Condition variables
Review: malloc()
demand paging.
Resizing heap: sbrk()
Designing malloc()
implicit linked list,explicit linked list
best t,rst t,next t
Problems:
fragmentation
memory leaks
valgrind tool=memcheck, checks for memory leaks.
Garbage collection
Review
Multithreaded programming
Concepts
Pthread
API
Mutex
Condition variables
simultaneously.
Instruction level (pipelining)
Data parallelism (SIMD)
Task parallelism (embarrassingly parallel)
parallel.
Concurrency vs. Parallelism
Network
Pipes, Queues
Multithreaded concurrency
Serial execution:
All our programs so far has had a single thread of
Multithreaded:
Program is organized as multiple and concurrent threads
of execution.
The main thread spawns multiple threads.
The thread may communicate with one another.
Advantages:
Improves performance
Improves responsiveness
Improves utilization
less overhead compared to multiple processes
6
Multithreaded programming
f l o a t params [ 1 0 ] ;
f o r ( i n t i =0; i <10; i ++)
do_something ( params [ i ] ) ;
f l o a t params [ 1 0 ] ;
f l o a t prev =0;
f o r ( i n t i =0; i <10; i ++)
{
prev= c o m p l i c a t e d ( params [ i ] , prev ) ;
}
paralleizable
not parallelizable
i n t balance =500;
void d e p o s i t ( i n t sum ) {
i n t c u r r b a l a n c e =balance ; / read balance /
...
c u r r b a l a n c e +=sum ;
balance= c u r r b a l a n c e ; / w r i t e balance /
}
void withdraw ( i n t sum ) {
i n t c u r r b a l a n c e =balance ; / read balance /
i f ( c u r r b a l a n c e >0)
c u r r b a l a n c e=sum ;
balance= c u r r b a l a n c e ; / w r i t e balance /
}
..
d e p o s i t ( 1 0 0 ) ; / t h r e a d 1 /
..
withdraw ( 5 0 ) ; / t h r e a d 2/
..
withdraw ( 1 0 0 ) ; / t h r e a d 3 /
...
Review
Multithreaded programming
Concepts
Pthread
API
Mutex
Condition variables
10
Pthread
API:
Thread management: creating, joining, attributes
pthread_
, pthread_barrier_
pthread_rwlock_
API:
#include <pthread.h>
10
Creating threads
i n t p t h r e a d _ c r e a t e ( p t h r e a d _ t thread ,
const p t h r e a d _ a t t r _ t a t t r ,
void ( s t a r t _ r o u t i n e ) ( void ) , void arg ) ;
thread of execution.
11
Example
void P r i n t H e l l o ( void t h r e a d i d )
{
long t i d ;
t i d = ( long ) t h r e a d i d ;
p r i n t f ( " H e l l o World ! I t s me, t h r e a d #%l d ! \ n " , t i d ) ;
p t h r e a d _ e x i t ( NULL ) ;
}
i n t main ( i n t argc , char argv [ ] )
{
p t h r e a d _ t t h r e a d s [NUM_THREADS ] ;
int rc ;
long t ;
f o r ( t =0; t <NUM_THREADS; t + + ) {
p r i n t f ( " I n main : c r e a t i n g t h r e a d %l d \ n " , t ) ;
i f ( r c ) {
p t h r e a d _ e x i t ( NULL ) ;
}
Lawrence Livermore National Laboratory. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse.
code: https://computing.llnl.gov/tutorials/pthreads/
Output
I n main : c r e a t i n g
I n main : c r e a t i n g
H e l l o World ! I t s
H e l l o World ! I t s
I n main : c r e a t i n g
I n main : c r e a t i n g
H e l l o World ! I t s
H e l l o World ! I t s
I n main : c r e a t i n g
H e l l o World ! I t s
thread 0
thread 1
me, t h r e a d
me, t h r e a d
thread 2
thread 3
me, t h r e a d
me, t h r e a d
thread 4
me, t h r e a d
#0!
#1!
#2!
#3!
#4!
I n main : c r e a t i n g
H e l l o World ! I t s
I n main : c r e a t i n g
H e l l o World ! I t s
I n main : c r e a t i n g
H e l l o World ! I t s
I n main : c r e a t i n g
H e l l o World ! I t s
I n main : c r e a t i n g
H e l l o World ! I t s
thread 0
me, t h r e a d
thread 1
me, t h r e a d
thread 2
me, t h r e a d
thread 3
me, t h r e a d
thread 4
me, t h r e a d
#0!
#1!
#2!
#3!
#4!
13
Synchronization: joining
Figure:
https://computing.llnl.gov/tutorials/pthreads
Lawrence Livermore National Laboratory. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse.
terminates.
If
14
Example
i n t r c ; long t ; void s t a t u s ;
/ I n i t i a l i z e and s e t t h r e a d detached a t t r i b u t e /
p t h r e a d _ a t t r _ i n i t (& a t t r ) ;
p t h r e a d _ a t t r _ s e t d e t a c h s t a t e (& a t t r , PTHREAD_CREATE_JOINABLE ) ;
i f ( r c ) {
/ Free a t t r i b u t e and w a i t f o r t h e o t h e r t h r e a d s /
p t h r e a d _ a t t r _ d e s t r o y (& a t t r ) ;
f o r ( t =0; t <NUM_THREADS; t ++) {
r c = p t h r e a d _ j o i n ( t h r e a d [ t ] , &s t a t u s ) ;
i f ( r c ) {
15
Mutex
The macro
PTHREAD_MUTEX_INITIALIZER
static mutexes.
pthread_mutex_destroy()
Mutex
pthread_mutex_unlock()
17
Example revisited
i n t balance =500;
void d e p o s i t ( i n t sum ) {
i n t c u r r b a l a n c e =balance ; / read balance /
...
c u r r b a l a n c e +=sum ;
balance= c u r r b a l a n c e ; / w r i t e balance /
}
void withdraw ( i n t sum ) {
i n t c u r r b a l a n c e =balance ; / read balance /
i f ( c u r r b a l a n c e >0)
c u r r b a l a n c e=sum ;
balance= c u r r b a l a n c e ; / w r i t e balance /
}
..
d e p o s i t ( 1 0 0 ) ; / t h r e a d 1 /
..
withdraw ( 5 0 ) ; / t h r e a d 2/
..
withdraw ( 1 0 0 ) ; / t h r e a d 3 /
...
Scenario: T1(read),T2(read,write),T1(write),balance=600
Scenario: T2(read),T1(read,write),T2(write),balance=450
18
Using mutex
i n t balance =500;
pthread_mutex_t mutexbalance=PTHREAD_MUTEX_INITIALIZER ;
void d e p o s i t ( i n t sum ) {
pthread_mutex_lock (& mutexbalance ) ;
{
i n t c u r r b a l a n c e =balance ; / read balance /
. . .
c u r r b a l a n c e +=sum ;
balance= c u r r b a l a n c e ; / w r i t e balance /
}
void withdraw ( i n t sum ) {
pthread_mutex_lock (& mutexbalance ) ;
{
i n t c u r r b a l a n c e =balance ; / read balance /
i f ( c u r r b a l a n c e >0)
c u r r b a l a n c e=sum ;
balance= c u r r b a l a n c e ; / w r i t e balance /
..
d e p o s i t ( 1 0 0 ) ; / t h r e a d 1 /
..
withdraw ( 5 0 ) ; / t h r e a d 2/
..
withdraw ( 1 0 0 ) ; / t h r e a d 3 /
Scenario: T1(read,write),T2(read,write),balance=550
Scenario: T2(read),T1(read,write),T2(write),balance=550
19
Condition variables
Condition variables
variable.
PTHREAD_COND_INITIALIZER
21
Condition variables
variable.
PTHREAD_COND_INITIALIZER
22
Condition variables
behavior undened.
automatically releases mutex
upon successful return, the mutex will be automatically
locked again.
int pthread_cond_broadcast(pthread_cond_t cond);
int pthread_cond_signal(pthread_cond_t cond);
Example
pthread_cond_t cond_recv=PTHREAD_COND_INITIALIZER ;
pthread_cond_t cond_send=PTHREAD_COND_INITIALIZER ;
pthread_mutex_t cond_mutex=PTHREAD_MUTEX_INITIALIZER ;
pthread_mutex_t count_mutex=PTHREAD_MUTEX_INITIALIZER ;
i n t f u l l =0;
i n t count =0;
while ( 1 )
while ( 1 )
{
{
24
Example
i n t main ( )
{
p t h r e a d _ t cons_thread , p r od _ th r ea d ;
p t h r e a d _ c r e a t e (& prod_thread , NULL , produce , NULL ) ;
p t h r e a d _ c r e a t e (& cons_thread , NULL , consume , NULL ) ;
p t h r e a d _ j o i n ( cons_thread , NULL ) ;
p t h r e a d _ j o i n ( prod_thread , NULL ) ;
return 0;
}
Output:
produced (3077516144):1
consumed (3069123440):1
produced (3077516144):2
consumed (3069123440):2
produced (3077516144):3
consumed (3069123440):3
produced (3077516144):4
consumed (3069123440):4
produced (3077516144):5
consumed (3069123440):5
produced (3077516144):6
consumed (3069123440):6
produced (3077516144):7
consumed (3069123440):7
25
Summary
26
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.