Pthread programming introduction - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Pthread programming introduction

Description:

SCHED_FIFO Denotes first-in first-out scheduling. SCHED_RR Denotes round-robin scheduling. ... Example -- Main Thread /* Continued ... – PowerPoint PPT presentation

Number of Views:183
Avg rating:3.0/5.0
Slides: 33
Provided by: mark172
Category:

less

Transcript and Presenter's Notes

Title: Pthread programming introduction


1
Pthread programming introduction
2
Threads and Processes
Reference https//computing.llnl.gov/tutorials/p
threads/
3
Thread Concept
Reference https//computing.llnl.gov/tutorials/p
threads/
4
Why Pthreads?
  • All threads within a process share the same
    address space. Inter-thread communication is more
    efficient and in many cases, easier to use than
    inter-process communication.
  • For Pthreads there is no intermediate memory copy
    required because threads share the same address
    space within a single process.

5
Pthread Flow
create
create
6
Pthread Creation and Termination
  • pthread_create(thread,attr,start_routine,arg)
  • Note that an "initial thread" exists by default
    and is the thread which runs main.
  • pthread_exit(status)
  • pthread_cancel(pthread_t thread)

7
Pthread Flow
create
create
8
Pthread Attribute Functions
  • Basic Management
  • pthread_attr_init(attr)
  • pthread_attr_destroy(attr)
  • pthread_attr_setschedpolicy(attr,schedpolicy)
  • SCHED_FIFO Denotes first-in first-out scheduling.
  • SCHED_RR Denotes round-robin scheduling.
  • SCHED_OTHER Denotes the default scheduling
    policy. It is the default value.

9
Scheduling Policy
  • pthread_attr_t sched_attr
  • pthread_attr_init(sched_attr)
  • pthread_attr_setschedpolicy(sched_attr,
    SCHED_FIFO)
  • pthread_create(thread,ched_attr,start_routine,arg
    )
  • pthread_attr_destroy(ched_attr)

10
Example Hello World !
include ltpthread.hgt include ltstdio.hgt define
NUM_THREADS 5 void PrintHello(void
threadid) int tid tid (int)threadid
printf("Hello World! It's me, thread d!\n",
tid) pthread_exit(NULL)
int main (int argc, char argv) pthread_t
threadsNUM_THREADS int rc, t for(t0
tltNUM_THREADS t) printf("In main creating
thread d\n", t) rc pthread_create(threadst
, NULL, PrintHello, (void )t) if (rc)
printf(ERROR return code from
pthread_create() is d\n", rc) exit(-1)
pthread_exit(NULL)
11
Result
In main creating thread 0 In main creating
thread 1 Hello World! It's me, thread 0! In
main creating thread 2 Hello World! It's me,
thread 1! Hello World! It's me, thread 2! In
main creating thread 3 In main creating thread
4 Hello World! It's me, thread 3! Hello World!
It's me, thread 4!
12
Passing Arguments To Threads
  • All arguments must be passed by reference and
    cast to (void ).

int taskidsNUM_THREADS for(t0t lt
NUM_THREADSt) taskidst (int )
malloc(sizeof(int)) taskidst t
printf("Creating thread d\n", t) rc
pthread_create(threadst, NULL, PrintHello,
(void ) taskidst) ...
13
Multiple Arguments Case
struct thread_data int thread_id int
sum char message struct thread_data
thread_data_arrayNUM_THREADS int main()
... thread_data_arrayt.thread_id t
thread_data_arrayt.sum sum
thread_data_arrayt.message messagest rc
pthread_create(threadst, NULL, PrintHello,
(void ) thread_data_arrayt) ...
14
When Passing Arguments
int rc, t for(t0t lt NUM_THREADSt)
printf("Creating thread d\n", t) rc
pthread_create(threadst, NULL, PrintHello,
(void ) t) ...
15
Pthread Join
  • pthread_join (threadid,status)
  • pthread_join() subroutine blocks the calling
    thread until the specified threadid thread
    terminates.

Referencehttps//computing.llnl.gov/tutorials/pt
hreads/
16
Join example
int rc, t for(t0t lt NUM_THREADSt)
printf("Creating thread d\n", t) rc
pthread_create(threadst, NULL, PrintHello,
(void ) t) ... for(t0 tltNUM_THREADS t)
rc pthread_join(threadt, status)
17
Race Condition
The balance is the critical section. We can use
mutex to prevent the race condition.
Referencehttp//www.mhpcc.edu/training/workshop2
/pthreads/
18
Solution to Race Condition
  • Mutex stands for Mutual exclusive.
  • Condition variables --- something works with
    Mutex.

19
Mutex Main Idea(Resource Sharing)
  • A mutex variable acts like a "lock" protecting
    access to a shared data resource.
  • The basic concept of a mutex as used in Pthreads
    is that only one thread can lock (or own) a mutex
    variable at any given time.

20
Mutex
  • Creating and Destroying Mutexes
  • pthread_mutex_init (mutex,attr)
  • pthread_mutex_destroy (mutex)
  • pthread_mutexattr_init (attr)
  • pthread_mutexattr_destroy (attr)
  • Locking / Unlocking Mutexes
  • pthread_mutex_lock (mutex)
  • pthread_mutex_trylock (mutex)
  • pthread_mutex_unlock (mutex)

21
Mutex Example
include ltpthread.hgt pthread_mutex_t
count_mutex int balance void deposit(int
cash) pthread_mutex_lock(count_mutex) Balance
balance cash pthread_mutex_unlock(count_mute
x) get_balance() int c pthread_mutex_lock(
count_mutex) c balance pthread_mutex_unlock(c
ount_mutex) return (c)
22
Dead Lock
Thread 2 ------------------------------------ pthr
ead_mutex_lock(m2) / use resource 2
/ pthread_mutex_lock(m1) / use resources 1
and 2 / pthread_mutex_unlock(m1) pthread_mute
x_unlock(m2)
Thread 1 ------------------------------------ pth
read_mutex_lock(m1) / use resource 1
/ pthread_mutex_lock(m2) / use resources 1
and 2 / pthread_mutex_unlock(m2) pthread_mute
x_unlock(m1)
23
Condition Variables
  • Without condition variables, the programmer would
    need to have threads continually polling, to
    check if the condition is met. This can be very
    resource consuming since the thread would be
    continuously busy in this activity. A condition
    variable is a way to achieve the same goal
    without polling.
  • A condition variable is always used in
    conjunction with a mutex lock.

24
Condition Variables
  • Creating and Destroying Condition Variables
  • pthread_cond_init (condition,attr)
  • pthread_cond_destroy (condition)
  • pthread_condattr_init (attr)
  • pthread_condattr_destroy (attr)
  • Waiting and Signaling on Condition Variables
  • pthread_cond_wait (condition,mutex)
  • pthread_cond_signal (condition)
  • pthread_cond_broadcast (condition)

25
(No Transcript)
26
Example -- Main Thread
include ltpthread.hgt include ltstdio.hgt define
NUM_THREADS 3 define TCOUNT 10 define
COUNT_LIMIT 12 int count 0 int
thread_ids3 0,1,2 pthread_mutex_t
count_mutex pthread_cond_t count_threshold_cv v
oid main() int i, rc pthread_t
threads3 pthread_attr_t attr /
Initialize mutex and condition variable objects
/ pthread_mutex_init(count_mutex, NULL)
pthread_cond_init (count_threshold_cv, NULL)
27
Main Thread Contd
/ Continued/ pthread_create(threads0,
attr, inc_count, (void )thread_ids0)
pthread_create(threads1, attr, inc_count,
(void )thread_ids1) pthread_create(threads
2, attr, watch_count, (void )thread_ids2)
/ Wait for all threads to complete / for
(i 0 i lt NUM_THREADS i)
pthread_join(threadsi, NULL) printf
("Main() Waited on d threads. Done.\n",
NUM_THREADS) / Clean up and exit /
pthread_attr_destroy(attr) pthread_mutex_destr
oy(count_mutex) pthread_cond_destroy(count_th
reshold_cv) pthread_exit (NULL)
28
void inc_count(void t) int j,i double
result0.0 int my_id (int)t for (i0 i
lt TCOUNT i) pthread_mutex_lock(count_mut
ex) count if (count COUNT_LIMIT)
printf("inc_count() thread d, count d
Threshold reached. ", my_id,
count) pthread_cond_signal(count_threshold
_cv) printf("Just sent signal.\n")
printf("inc_count() thread d, count d,
unlocking mutex\n", my_id, count)
pthread_mutex_unlock(count_mutex) / Do
some work so threads can alternate on mutex lock
/ for (j0 j lt 10000 j) result
result (double)random()
pthread_exit(NULL)
Thread 0 1
29
void watch_count(void idp) int my_id
idp printf("Starting watch_count() thread
d\n", my_id) pthread_mutex_lock(count_mutex)
while (count lt COUNT_LIMIT)
pthread_cond_wait(count_threshold_cv,
count_mutex) printf("watch_count() thread
d Condition signal received.\n",
my_id) pthread_mutex_unlock(count_mutex
) pthread_exit(NULL)
Thread 2
30
Result
inc_count() thread 0, count 1, unlocking
mutex Starting watch_count() thread
2 inc_count() thread 1, count 2, unlocking
mutex inc_count() thread 0, count 3, unlocking
mutex inc_count() thread 1, count 4, unlocking
mutex inc_count() thread 0, count 5, unlocking
mutex inc_count() thread 0, count 6, unlocking
mutex inc_count() thread 1, count 7, unlocking
mutex inc_count() thread 0, count 8, unlocking
mutex inc_count() thread 1, count 9, unlocking
mutex inc_count() thread 0, count 10,
unlocking mutex inc_count() thread 1, count
11, unlocking mutex inc_count() thread 0, count
12 Threshold reached. inc_count() thread 0,
count 12, unlocking mutex watch_count() thread
2 Condition signal received. inc_count() thread
1, count 13, unlocking mutex inc_count()
thread 0, count 14, unlocking
mutex inc_count() thread 1, count 15,
unlocking mutex inc_count() thread 0, count
16, unlocking mutex inc_count() thread 1, count
17, unlocking mutex inc_count() thread 0,
count 18, unlocking mutex inc_count() thread
1, count 19, unlocking mutex inc_count()
thread 1, count 20, unlocking mutex Main()
Waited on 3 threads. Done.
31
Compile and Run
  • Compile
  • gcc o a.out program.c -lpthread
  • Run
  • ./a.out

32
Reference
  • POSIX Threads Programming
  • https//computing.llnl.gov/tutorials/pthreads/
  • http//www.mhpcc.edu/training/workshop2/pthreads
Write a Comment
User Comments (0)
About PowerShow.com