Programming With POSIX Threads - PPT
Programming With POSIX Threads - PPT
Objectives
2
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
1
What is Pthreads?
C language interface
• programming types and procedure calls
• implemented as standalone library or as part of another library
such as libc
3
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Pthreads API:
• Thread management: creating, detaching, joining, etc.
• Mutexes: deal with synchronization
• Condition variables: communications between threads that
share a mutex
4
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
2
pthread_create
pthread_t *tid
• handle of created thread
const pthread_attr_t *attr
• attributes of thread to be created
void *(*function)(void *)
• function to be mapped to thread
void *arg
• single argument to function
Compare with Win32
CreateThread
Programming with POSIX* Threads
5
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
pthread_create Explained
6
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
3
Example: Thread Creation
#include <stdio.h>
#include <pthread.h>
main() {
pthread_t tid;
What Happens?
Programming with POSIX* Threads
7
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
pthread_t tid
• handle of joinable thread
void **val_ptr
• exit value returned by joined thread
8
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
4
pthread_join Explained
9
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Thread States
No equivalent for
Win32 Threads
Programming with POSIX* Threads
10
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
5
Example: Multiple Threads
#include <stdio.h>
#include <pthread.h>
#define NUM_THREADS 4
main() {
pthread_t tid[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++)
pthread_create(&tid[i], NULL, hello, NULL);
11
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
What’s Wrong?
void *threadFunc(void
*threadFunc(void *pArg)
) {
int*
int* p = (int
(int*)
*)pArg
pArg;
;
int myNum = *p;
printf( “Thread number %d\n”, myNum);
}
. . .
// from main():
for (int
(int i = 0; i < numThreads;
numThreads; i++) {
pthread_create(&tid[i],
pthread_create(&tid[i], NULL, threadFunc,
threadFunc, &i);
}
12
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
6
Solution – “Local” Storage
void *threadFunc(void
*threadFunc(void *pArg)
)
{
int myNum = *(((int*)
int*)pArg
pArg)
);
printf( “Thread number %d\n”, myNum);
}
. . .
// from main():
for (int
(int i = 0; i < numThreads;
numThreads; i++) {
tNum[i]
tNum[i] = i;
pthread_create(&tid[i],
pthread_create(&tid[i], NULL, threadFunc,
threadFunc, &tNum[i]
tNum[i]);
}
13
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
14
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
7
pthread_mutex_init
pthread_mutex_t *mutex
• mutex to be initialized
const pthread_mutexattr_t *attr
• attributes to be given to mutex
15
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Alternate Initialization
16
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
8
pthread_mutex_lock
pthread_mutex_t *mutex
• mutex to attempt to lock
17
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
pthread_mutex_lock Explained
18
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
9
pthread_mutex_unlock
pthread_mutex_t *mutex
• mutex to be unlocked
19
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
main() {
pthread_t hThread[NUMTHREADS];
20
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
10
Condition Variables
21
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
22
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
11
Lost and Spurious Signals
23
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Negation of condition
needed to proceed
acquire mutex;
while (conditional is true) Mutex is automatically
released when thread
wait on condition variable; waits
24
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
12
Condition Variables
pthread_cond_init, pthread_cond_destroy
• initialize/destroy condition variable
pthread_cond_wait
• thread goes to sleep until signal of condition variable
pthread_cond_signal
• signal release of condition variable
pthread_cond_broadcast
• broadcast release of condition variable
25
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
• pthread_condattr_t
• condition variable attributes
26
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
13
pthread_cond_init
pthread_cond_t *cond
• condition variable to be initialized
const pthread_condattr_t *attr
• attributes to be given to condition variable
27
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
Alternate Initialization
28
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
14
pthread_cond_wait
pthread_cond_t *cond
• condition variable to wait on
pthread_mutex_t *mutex
• mutex to be unlocked
29
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
pthread_cond_wait Explained
30
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
15
pthread_cond_signal
pthread_cond_t *cond
• condition variable to be signaled
31
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
pthread_cond_signal Explained
32
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
16
pthread_cond_broadcast
pthread_cond_t *cond
• condition variable to signal
33
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
pthread_cond_broadcast Explained
34
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
17
Programming with POSIX* Threads
What’s Been Covered
How to create threads to execute work encapsulated within
functions
Coordinate shared access between threads to avoid race
conditions
35
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
36
Copyright © 2006, Intel Corporation. All rights reserved.
Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners.
18