POSIX Threads(pthreads)

POSIX Threads(pthreads)

POSIX Threads(pthreads) Paraleln potaov systmy Pavol Hudako, Soa Kolrova, Lucia Michalenkov 2018/2019 Links Presentation: https://goo.gl/8LTe7z Programs: https://goo.gl/N4zr18 POSIX Threads (pthreads) libraries standard based thread API for C/C++, defined by standard IEEE

POSIX 1003.1c-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_ Thread Management - Creation #include

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. 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 int pthread_exit(void *retval); int pthread_cancel(pthread_t thread); 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 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 /*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); Thread Synchronization - Mutex example

Thread Synchronization - Joins A join is performed when one wants to wait for a thread to finish. (Thread must by Joinable) #include 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.

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.

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); Time results - Matrix Time results - Bubble sort Visual Studio configuration 1. ) Download: http://cs.du.edu/~mitchell/pthreads_compiled.zip

Zdroje - https://en.wikipedia.org/wiki/POSIX_Threads http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html https://computing.llnl.gov/tutorials/pthreads/ http://man7.org/linux/man-pages/man3/

Recently Viewed Presentations

  • Imaging Spectrum of Post Therapy Related Disorders: A

    Imaging Spectrum of Post Therapy Related Disorders: A

    Anti-CTL4 antibody therapy in cancer. Epidural Lipomatosis and Exogenous Steroids . Not a rare condition. Radiographic diagnosis. Excess deposition of normal adipose tissue in the epidural space. ... Lewis-Sumner syndrome. Small fiber sensory neuropathy.
  • Write down the Reactivity Series from Potassium to

    Write down the Reactivity Series from Potassium to

    A metal will displace (take the place of) a less reactive metal in metal salt solution. What would you See during the Reaction? Copper(II) sulphate is blue, iron sulphate is colourless. During the reaction the blue solution loses its colour...
  • Chapter 1: What is Economics?

    Chapter 1: What is Economics?

    Chapter 1: What is Economics? Chapter 1 Sec 1 Scarcity and the factors of production How does scarcity force people to make economic decisions? How does scarcity force people to make economic choices? Scarcity forces all of us to make...
  • Fta Substance Abuse Management Oversight Third Quarter Report

    Fta Substance Abuse Management Oversight Third Quarter Report

    FTA TRAINING SEMINARS ON PART 655 Sponsored by: Federal Transit Administration Overview of Training Testing categories -- when must a transit system conduct tests?
  • Delivering effective clinical harm reviews

    Delivering effective clinical harm reviews

    Delivering effective clinical harm reviews. Prof Andrew Chilton MD KGH FT. Dr Sanjay Ghardia GP Corby CCG. Ms Clare Smith Harm Coordinator KGH FT
  • Senior School - Mock Parliament Competition

    Senior School - Mock Parliament Competition

    Final Round. MPI: That the Senate supports reform to require at a early stage of a court case, a private litigant will be able to request a 'no adverse cost order', preventing large legal fees of the defendant being transferred...
  • Translation and transcription!?! - rachelhawkes.com

    Translation and transcription!?! - rachelhawkes.com

    An awareness that translation is not straightforward and can lead to misunderstandings. An introduction to non-literal translation, promoting mental agility and linguistic precision. A better conscious understanding of linguistic structures (grammar) in use. A higher level of intercultural appreciation
  • Management Planning and Control Systems

    Management Planning and Control Systems

    Management Planning and Control System encompass " A Financial or quantitative statement prepared prior to a defined period of time of a policy to be prepared for that period to attain Short Term(Action Plan) and Long Term(Strategic Plan)of the organization".