Download presentation
Presentation is loading. Please wait.
1
POSIX Threads(pthreads)
Paralelné počítačové systémy Pavol Hudačko, Soňa Kolárova, Lucia Michalenková 2018/2019
2
Links Presentation: https://goo.gl/8LTe7z
Programs:
3
POSIX Threads (pthreads) libraries
standard based thread API for C/C++, defined by standard IEEE POSIX c-1995 available for UNIX, but also non UNIX systems, for Windows as pthreads-w32 implemented with a pthread.h, all procedures prefixed with pthread_ The POSIX semaphore API works with POSIX threads, but is not part of threads standard, but in semaphore.h, procedures prefixed with sem_
4
Thread Management - Creation
#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); Arguments: thread - returns the thread id attr - Set to NULL if default thread attributes are used void * (*start_routine) - pointer to the function to be threaded. Function has a single argument: pointer to void. *arg - pointer to argument of function. To pass multiple arguments, send a pointer to a structure.
5
Thread Management - Termination
The thread returns normally from its starting routine. Its work is done. The thread makes a call to the pthread_exit subroutine - whether its work is done or not. The thread is canceled by another thread via the pthread_cancel routine. The entire process is terminated due to making a call to either the exec() or exit() If main() finishes first, without calling pthread_exit explicitly itself #include <pthread.h> int pthread_exit(void *retval); int pthread_cancel(pthread_t thread);
6
Thread Synchronization
Critical section a piece of code that only one thread can execute at a time 3 synchronization mechanisms: mutexes - Mutual exclusion lock: Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables. joins - Make a thread wait till others are complete (terminated). condition variables - data type pthread_cond_t
7
Thread Synchronization - Mutex lock/unlock
Mutual exclusion lock: Block access to variables by other threads A thread should lock before entering a critical section A thread should unlock when leaving the critical section At one time, only one thread can get the lock #include <pthread.h> /*acquire a lock on the specified mutex variable. If the mutex is already locked by another thread, this call will block the calling thread until the mutex is unlocked*/ int pthread_mutex_lock(pthread_mutex_t *mutex); /* attempt to lock a mutex or will return error code if busy. Useful for preventing deadlock conditions.*/ int pthread_mutex_trylock(pthread_mutex_t *mutex); /*unlock a mutex variable. An error is returned if mutex is already unlocked or owned by another thread*/ int pthread_mutex_unlock(pthread_mutex_t *mutex);
8
Thread Synchronization - Mutex example
9
Thread Synchronization - Joins
A join is performed when one wants to wait for a thread to finish. (Thread must by Joinable) #include <pthread.h> int pthread_join(pthread_t thread, void **retval); Arguments: thread - thread suspended until the thread identified by thread terminates, either by calling pthread_exit() or by being cancelled. retval - If retval is not NULL, the return value of th is stored in the location pointed to by retval.
10
Thread Synchronization - Condition variables
condition variables allow threads to synchronize based on the value of the data helps to avoid continually polling always must be associated with a mutex to avoid a race condition created by one thread preparing to wait and another thread which may signal the condition before the first thread actually waits on it resulting in a deadlock. two threads wanting to wait for the same condition variable must use the same mutex.
11
Thread Synchronization - Condition variables
Creating/Destroying: int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr); int pthread_cond_destroy(pthread_cond_t *cond); Waiting on condition: /*unlocks the mutex and waits for the condition variable cond to be signaled*/ int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); /* place limit on how long it will block */ int pthread_cond_timedwait(pthread_cond_t *restrict cond,pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime); Waking thread based on condition: /* restarts one of the threads that are waiting on the condition variable cond. */ int pthread_cond_signal(pthread_cond_t *cond); /* wake up all threads blocked by the specified condition variable */ int pthread_cond_broadcast(pthread_cond_t *cond);
12
Time results - Matrix
13
Time results - Bubble sort
14
Visual Studio configuration
1. ) Download:
15
Zdroje https://en.wikipedia.org/wiki/POSIX_Threads
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.