POSIX threads and C++ facilities Jakub Yaghob. Low-level threading and synchronization support Pthreads POSIX threads IEEE POSIX 1003.1c (1995) C library.

Slides:



Advertisements
Similar presentations
CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Advertisements

1 Threads. 2 Real Life Example?  Process  “system programming” course  Different from “internet engineering”  Thread  homework, Reading, Self-assessment.
Pthreads & Concurrency. Acknowledgements  The material in this tutorial is based in part on: POSIX Threads Programming, by Blaise Barney.
Multi-core Programming Programming with Posix Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
MultiCore Processing Workshop Multithreaded Programming using POSIX Threads(Pthreads) Syed Akbar Mehdi.
MultiCore Processing Workshop Multithreaded Programming using POSIX Threads(Pthreads) Syed Akbar Mehdi.
Chap. 23 Threads (1) Threads v.s. Fork (2) Basic Thread Functions #include int pthread_create ( pthread_t *tid, const pthread_attr_t *attr, void *(*func)(void.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
POSIX Threads Nezer J. Zaidenberg. References  Advanced programming for the UNIX environment (2nd edition. This material does not exist in first edition)
Parallel and Cluster Computing
Pthreads Operating Systems Hebrew University of Jerusalem Spring 2004.
8-1 JMH Associates © 2004, All rights reserved Windows Application Development Chapter 10 - Supplement Introduction to Pthreads for Application Portability.
Lecture 18 Threaded Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.
Threads? Threads allow us to have multiple tasks active at the same time in one executable –think of a server handling multiple connections Each thread.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
Many-SC Programming Model Jaejin Lee Center for Manycore Programming Seoul National University
Introduction to Pthreads. Pthreads Pthreads is a POSIX standard for describing a thread model, it specifies the API and the semantics of the calls. Model.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
04/10/25Parallel and Distributed Programming1 Shared-memory Parallel Programming Taura Lab M1 Yuuki Horita.
Operating Systems CMPSC 473 Multi-threading models Tutorial on pthreads Lecture 10: September Instructor: Bhuvan Urgaonkar.
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
POSIX Threads Nezer J. Zaidenberg. References  Advanced programming for the UNIX environment (2nd edition chapter This material does not exist.
Threads and Thread Control Thread Concepts Pthread Creation and Termination Pthread synchronization Threads and Signals.
Copyright ©: University of Illinois CS 241 Staff1 Synchronization and Semaphores.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
What is a thread? process: an address space with 1 or more threads executing within that address space, and the required system resources for those threads.
Includes slides from course CS194 at UC Berkeley, by prof. Katherine Yelick Shared Memory Programming Pthreads: an overview Ing. Andrea Marongiu
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.
Programming with POSIX* Threads Intel Software College.
Condition Variables u A condition variable is used to wait until a particular condition is true  like in a monitor u Always use condition variables together.
Pthreads: A shared memory programming model
Threads CSCE Thread Motivation Processes are expensive to create. Context switch between processes is expensive Communication between processes.
CSC Advanced Unix Programming, Fall, 2008 Monday, November 24 POSIX threads (pthreads)
S -1 Posix Threads. S -2 Thread Concepts Threads are "lightweight processes" –10 to 100 times faster than fork() Threads share: –process instructions,
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
UNIX Socket Programming CS 6378 Project Reference Book: Unix Network programming: Networking APIs: Sockets and XTI (2nd edition), Prentice Hall >> Threads.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
(p)Threads Libraries Math 442 es Jim Fix. Life cycle of a thread.
POSIX Threads HUJI Spring 2011.
Pthreads.
P4: Multithreaded Programming Zhenxiao Luo CS537 Spring 2010.
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
Working with Pthreads. Operations on Threads int pthread_create (pthread_t *thread, const pthread_attr_t *attr, void * (*routine)(void*), void* arg) Creates.
pThread synchronization
C++ - parallelization and synchronization
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
A thread is a basic unit of CPU utilization within a process Each thread has its own – thread ID – program counter – register set – stack It shares the.
Synchronization and Semaphores
CS 537 – Introduction to Operating Systems
C Threads and Semaphores
Linux Thread Programming
C++11 Threading Lieven de Cock
Threads Threads.
Netprog: Threads Programming
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Thread Programming.
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Operating Systems Lecture 13.
PTHREADS AND SEMAPHORES
Thread Programming.
Synchronization Primitives – Semaphore and Mutex
Multithreading Tutorial
POSIX Threads(pthreads)
Presentation transcript:

POSIX threads and C++ facilities Jakub Yaghob

Low-level threading and synchronization support Pthreads POSIX threads IEEE POSIX c (1995) C library All POSIX compliant systems (even Windows) ISO C Standard libraries New compilers Currently only partial support

Pthreads overview Thread management Thread attributes Mutexes Condition variables Synchronization R/W locks, barriers

Threads Create attr==NULL – default attributes int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); Exit Return value_ptr to join void pthread_exit(void *value_ptr); Join Suspend calling thread and wait for exit int pthread_join(pthread_t thread, void **value_ptr);

Threads – join

Threads – joinable, detached Joinable Only joinable threads can be joined Should be by default joinable Explicitly set joinability for greater compatibility Detached Explicitly set by attributes Cannot be joined Some resources can be spared

Threads – misc Get my ID pthread_t pthread_self(void); Compare thread IDs int pthread_equal(pthread_t t1, pthread_t t2); Initialize once int pthread_once(pthread_once_t *once_control, void (*init_routine)(void)); pthread_once_t once_control = PTHREAD_ONCE_INIT; Kill int pthread_kill(pthread_t thread, int sig);

Thread attributes Initialize attributes int pthread_attr_init(pthread_attr_t *attr); Destroy attributes int pthread_attr_destroy(pthread_attr_t *attr); Set/get int pthread_attr_getXXX(const pthread_attr_t *attr, TTT *av); int pthread_attr_setXXX(pthread_attr_t *attr, TTT av);

Thread attributes – examples Joinable/detached XXX = detachstate, TTT = int, av = PTHREAD_CREATE_DETACHED or PTHREAD_CREATE_JOINABLE Stack XXX = stacksize, TTT = size_t XXX = stackaddr, TTT = void* Scheduling inheritsched, schedparam, schedpolicy

Mutexes Create int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); Destroy int pthread_mutex_destroy(pthread_mutex_t *mutex); Attributes Protocol, sharing, … int pthread_mutexattr_init(pthread_mutexattr_t *attr); int pthread_mutexattr_destroy(pthread_mutexattr_t *attr); int pthread_mutexattr_getXXX(const pthread_mutexattr_t *attr, TTT *av); int pthread_mutexattr_setXXX(pthread_mutexattr_t *attr, TTT av);

Mutexes – locking Blocking lock int pthread_mutex_lock(pthread_mutex_t *mutex); Non-blocking lock int pthread_mutex_trylock(pthread_mutex_t *mutex); Unlock int pthread_mutex_unlock(pthread_mutex_t *mutex);

Condition variables Create int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); Destroy int pthread_cond_destroy(pthread_cond_t *cond); Attributes Clock, sharing, … int pthread_condattr_destroy(pthread_condattr_t *attr); int pthread_condattr_init(pthread_condattr_t *attr); int pthread_condattr_getXXX(const pthread_condattr_t *attr, TTT *av); int pthread_condattr_setXXX(pthread_condattr_t *attr, TTT av);

Condition variables – locking Blocking wait int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); Blocking timed wait int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); Unblock one int pthread_cond_signal(pthread_cond_t *cond); Unblock all int pthread_cond_broadcast(pthread_cond_t *cond);

R/W lock Create int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr); Destroy int pthread_rwlock_destroy(pthread_rwlock_t *rwlock); Attributes Sharing, … int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr); int pthread_rwlockattr_init(pthread_rwlockattr_t *attr); int pthread_rwlockattr_getXXX(const pthread_rwlockattr_t *attr, TTT *av); int pthread_rwlockattr_setXXX(pthread_rwlockattr_t *attr, TTT av);

R/W lock – reader Blocking lock int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); Non-blocking lock int thread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); Timed lock int thread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, const struct timespec *abs_timeout); Unlock int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

R/W lock – writer Blocking lock int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); Non-blocking lock int thread_rwlock_trywrlock(pthread_rwlock_t *rwlock); Timed lock int thread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, const struct timespec *abs_timeout); Unlock int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);

Barrier Init int pthread_barrier_init(pthread_barrier_t *barrier, const pthread_barrierattr_t *attr, unsigned count); Destroy int pthread_barrier_destroy(pthread_barrier_t *barrier); Wait int pthread_barrier_wait(pthread_barrier_t *barrier); Attributes Sharing, … int pthread_barrierattr_destroy(pthread_barrierattr_t *attr); int pthread_barrierattr_init(pthread_barrierattr_t *attr); int pthread_barrierattr_getXXX(const pthread_barrierattr_t *attr, TTT *av); int pthread_barrierattr_setXXX(pthread_barrierattr_t *attr, TTT av);

Spin-lock Create int pthread_spin_init(pthread_spinlock_t *lock, int pshared); Destroy int pthread_spin_destroy(pthread_spinlock_t *lock); Wait int pthread_spin_lock(pthread_spinlock_t *lock); Non-blocking wait int pthread_spin_trylock(pthread_spinlock_t *lock); Unblock int pthread_spin_unlock(pthread_spinlock_t *lock);

Thread local storage Create dest_routine called at thread exit int pthread_key_create(pthread_key_t * key, void (* dest_routine(void *))); Destroy int pthread_key_delete(pthread_key_t key); Set int pthread_setspecific(pthread_key_t key, const void * pointer); Get void * pthread_getspecific(pthread_key_t key);

ISO C overview Threads Mutexes Condition variables Atomic variables Futures A future is a token for a value that will be available later Focus on communication between threads Synchronization details left to library

Futures std::future Defines a type for asynchronous return object which do not share their shared state std::shared_future Like future but may share their shared state std::promise Explicitly set shared state std::packaged_task Shared state is the result of a function call std::async Launching a function potentially in a new thread

Futures – example double comp(vector & v) { // package the tasks: // (the task here is the standard accumulate() for an array of doubles): packaged_task pt0{std::accumulate }; packaged_task pt1{std::accumulate }; auto f0 = pt0.get_future(); // get hold of the futures auto f1 = pt1.get_future(); pt0(&v[0],&v[v.size()/2],0); // start the threads pt1(&[v.size()/2],&v[size()],0); return f0.get()+f1.get(); // get the results }

Futures – async example template struct Accum { // simple accumulator function object T* b; T* e; V val; Accum(T* bb, T* ee, const V& v) : b{bb},e{ee},val{vv} {} V operator() () { return std::accumulate(b,e,val); } }; double comp(vector & v) { // spawn many tasks if v is large enough if (v.size()<10000) return std::accumulate(v.begin(),v.end(),0.0); auto f0 {async(Accum{&v[0],&v[v.size()/4],0.0})}; auto f1 {async(Accum{&v[v.size()/4],&v[v.size()/2],0.0})}; auto f2 {async(Accum{&v[v.size()/2],&v[v.size()*3/4],0.0})}; auto f3 {async(Accum{&v[v.size()*3/4],&v[v.size()],0.0})}; return f0.get()+f1.get()+f2.get()+f3.get(); }

Threads Simple low-level access std::thread Namespace this_thread thread::id get_id() void yield() void sleep_until(abs_time) void sleep_for(rel_time) bool joinable() const void join() void detach()

Threads – example void f(); struct F { void operator()(); }; int main() { std::thread t1{f}; // f() executes in separate thread std::thread t2{F()}; // F()() executes in separate thread t1.join(); // wait for t1 t2.join(); // wait for t2 }

Mutexes Classes std::mutex std::recursive_mutex std::timed_mutex std::recursive_timed_mutex Operations void m.lock() bool m.try_lock() void m.unlock() Timed mutexes operations bool tm.try_lock_for(rel_time) bool tm.try_lock_until(abs_time)

Condition variables Class std::condition_variable void notify_one() void notify_all() void wait() bool wait_for(rel_time) bool wait_until(abs_time) Class std::condition_variable_any Use any mutex

Locks Class std::lock_guard Scope lock Class std::unique_lock Controls ownership Generic lock template void lock(L1&, L2&, L3&...); Call once class information { std::once_flag verified; void verifier(); public: void verify() { std::call_once(verified,verifier); } };

Atomic variables Generic template atomic compare_exchange load, store Specialization for basic types fetch_add, fetch_sub, fetch_and, fetch_or, fetch_xor Class atomic_flag bool test_and_test() Fences