Академический Документы
Профессиональный Документы
Культура Документы
Review
Multithreaded Programming
Race Conditions
Semaphores
Thread Safety, Deadlock, and Starvation
Sockets and Asynchronous I/O
Sockets
Asynchronous I/O
memory
Program organized to execute multiple threads in parallel
Threads spawned by main thread, communicate via
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 ) ;
locks, barriers
Mutex: simple single lock/unlock mechanism
variable
Review
Multithreaded Programming
Race Conditions
Semaphores
Thread Safety, Deadlock, and Starvation
Sockets and Asynchronous I/O
Sockets
Asynchronous I/O
Multithreaded programming
execute when
Scheduling may execute threads in arbitrary order
Without proper synchronization, code can execute
non-deterministically
Suppose we have two threads: 1 reads a variable, 2
Race conditions
T2
condition variable forces thread T2 to wait for thread T1
producer-consumer model program
f o r ( i = 0 ; i < 4 ; i ++)
p t h r e a d _ j o i n ( t i d s [ i ] , NULL ) ;
r e t u r n 0 ;
Prentice Hall. All rights reserved. This content is excluded from our Creative Commons license.
For more information, see http://ocw.mit.edu/fairuse.
Athena is MIT's UNIX-based computing environment. OCW does not provide access to it.
c n t ++;
}
r e t u r n NULL ;
}
i n t main ( void ) {
pthread_t t i d s [ 4 ] ;
int i ;
p t h r e a d _ m u t e x _ i n i t (& mutex , NULL ) ;
f o r ( i = 0 ; i < 4 ; i ++)
p t h r e a d _ c r e a t e (& t i d s [ i ] , NULL , count , NULL ) ;
f o r ( i = 0 ; i < 4 ; i ++)
p t h r e a d _ j o i n ( t i d s [ i ] , NULL ) ;
r e t u r n 0 ;
Race conditions
10
Semaphores
pthread
11
Using semaphores
Destroy semaphore:
int sem_destroy(sem_t sem);
otherwise):
int sem_trywait(sem_t sem);
12
synchronous
13
sem_wait (& i t e m s ) ;
sem_t mutex , s l o t s , i t e m s ;
# define SLOTS 2
# define ITEMS 10
void produce ( void arg )
{
int i ;
f o r ( i = 0 ; i < ITEMS ; i ++)
{
sem_wait (& s l o t s ) ;
}
i n t main ( )
p t h r e a d _ t tcons , t p r o ;
s e m _ i n i t (& mutex , 0 , 1 ) ;
s e m _ i n i t (& s l o t s , 0 , SLOTS ) ;
s e m _ i n i t (& items , 0 , 0 ) ;
p t h r e a d _ c r e a t e (& tcons , NULL , consume , NULL ) ;
p t h r e a d _ c r e a t e (& t p r o , NULL , produce , NULL ) ;
p t h r e a d _ j o i n ( tcons , NULL ) ;
p t h r e a d _ j o i n ( t p r o , NULL ) ;
14
Other challenges
deadlock
starvation
15
Thread safety
16
Reentrant functions
functions reentrant?)
Reentrant versions of many unsafe C standard library
functions exist:
Unsafe function
rand()
strtok()
asctime()
ctime()
gethostbyaddr()
gethostbyname()
inet_ntoa()
localtime()
Reentrant version
rand_r()
strtok_r()
asctime_r()
ctime_r()
gethostbyaddr_r()
gethostbyname_r()
(none)
localtime_r()
17
Thread safety
18
Deadlock
objects
Tricky bug to locate and reproduce, since
schedule-dependent
Can visualize using a progress graph traces progress of
19
Deadlock
20
Deadlock
21
22
Review
Multithreaded Programming
Race Conditions
Semaphores
Thread Safety, Deadlock, and Starvation
Sockets and Asynchronous I/O
Sockets
Asynchronous I/O
23
Sockets
library)
Network I/O, due to latency, usually implemented
connections
23
Creating a socket
24
Connecting to a server
returns 0 if successful
25
26
27
queued):
int accept(int fd , struct sockaddr addr, int addr_len);
fd sockets le descriptor
addr pointer to structure to be lled with client address
28
fd sockets le descriptor
29
Asynchronous I/O
le descriptors
30
tested (0 up to nfds1)
readfds, writefds, errorfds if not NULL, pointer to
31
FD_ZERO(&fdset)
descriptors
FD_CLR(fd, &fdset)
32
33
Summary
Multithreaded programming
race conditions
semaphores
thread safety
deadlock and starvation
Sockets, asynchronous I/O
client/server socket functions
select() and poll()
34
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.