Thread synchronization

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
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.
Monitors CSCI 444/544 Operating Systems Fall 2008.
Pthread II. Outline Join Mutex Variables Condition Variables.
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
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.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Advanced Programming Rabie A. Ramadan Lecture 7. Multithreading An Overview 2 Some of the slides are exerted from Jonathan Amsterdam presentation.
Multi-threaded Programming with POSIX Threads CSE331 Operating Systems Design.
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.
ICS 145B -- L. Bic1 Project: Process/Thread Synchronization Textbook: pages ICS 145B L. Bic.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Semaphore and Mutex Operations.
Programming with POSIX* Threads Intel Software College.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Pthreads: A shared memory programming model
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Threads Chapter 26. Threads Light-weight processes Each process can have multiple threads of concurrent control. What’s wrong with processes? fork() is.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
POSIX Synchronization Introduction to Operating Systems: Discussion Module 5.
Synchronizing Threads with Semaphores
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
CS 360 pthreads Condition Variables for threads. Page 2 CS 360, WSU Vancouver What is the issue? Creating a thread to perform a task and then joining.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
pThread synchronization
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Synchronization and Semaphores
CS703 - Advanced Operating Systems
PThreads.
Principles of Operating Systems Lecture 11
Atomic Operations in Hardware
Multithreading Tutorial
Atomic Operations in Hardware
5-High-Performance Embedded Systems using Concurrent Process (cont.)
Synchronization Lecture 23 – Fall 2017.
Lecture 14: Pthreads Mutex and Condition Variables
Condition Variables and Producer/Consumer
PTHREADS AND SEMAPHORES
Condition Variables and Producer/Consumer
Multithreading Tutorial
Producer-Consumer Problem
Jonathan Walpole Computer Science Portland State University
Pthread Prof. Ikjun Yeom TA – Mugyo
Chapter 30 Condition Variables
Multithreading Tutorial
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Synchronization Primitives – Semaphore and Mutex
Multithreading Tutorial
Lecture 14: Pthreads Mutex and Condition Variables
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Lecture 20: Synchronization
EECE.4810/EECE.5730 Operating Systems
Software Engineering and Architecture
Don Porter Portions courtesy Emmett Witchel
POSIX Threads(pthreads)
Presentation transcript:

Thread synchronization Mutex Conditional variables

Thread synchronization Threads in mm_pthread.c and pi_pthread.c have very minor interactions. All computations are independent (essential for parallel execution) Dependencies in a program can cause problems in parallel execution. for (i=0; i<100; i++) for(i=0; i<100; i++) a[i] = 0; a[i] = a[i-1]+1;

Thread synchronization Most of threaded programs have threads that interact with one another. Interaction in the form of sharing access to variables. Multiple concurrent reads (ok) Multiple concurrent writes (not ok, outcome non-deterministic) One write, multiple reads (not ok, outcome non-deterministic) Needs to make sure that the outcome is deterministic. Synchronization: allowing concurrent accesses to variables, removing non-deterministic outcome by enforcing the order of thread execution.

Thread synchronization Typical types of synchronizations. Mutual exclusion (mutex in pthread): Thread 2: insert B to tree Thread 1: insert A to tree Thread 2: lock(tree) insert A to tree unlock(tree) Thread 1: lock(tree) insert A to tree unlock(tree)

Thread synchronization Signal (ordering the execution of threads, condition variable) Thread 1: Thread 2: Thread 3: for (i=0; i<25; i++) for (i=25; i<50; i++) for (i=50; i<75;i++) a(i+1) = a(i)+1 a(i+1) = a(i) +1; a(i+1) = a(i)+1; Thread 1: Thread 2: Thread 3: for (i=0; i<25; i++) a(i+1) = a(i)+1 signal a(25) ready wait for a(25) ready for(i=25;i<50;i++) signal a(50) ready wait for a(50) ready ……

A pthread example (example1.c) int counter = 0; void *thread_producer(void *arg) { int val; /* produce a product */ counter++; return NULL; } Could there be any problem in this code?

An example (example1.c) int counter = 0; void *thread_producer(void *arg) { int val; /* produce a product */ counter++; /* this may not be atomic */ return NULL; } Most constructs in the high level language are not atomic!! Need to make them atomic explicitly in a threaded program. Solution: mutex

Mutex variables Mutex = abbreviation for “mutual exclusion” Primary means of implementing thread synchronization and protecting shared data with multiple concurrent writes. A mutex variable acts like a lock Multple threads can try to lock a mutex, only one will be successful; other threads will be blocked until the owning thread unlock that mutex.

Mutex variables A typical sequence in the use of a mutex is as follows: Create and initialize a mutex variable Several threads attempt to lock the mutex Only one succeeds and that thread owns the mutex The owner thread performs some set of actions The owner unlocks the mutex Another thread acquires the mutex and repeats the process Finally the mutex is destroyed

Mutex operations Creation: Destroying: Locking and unlocking mutexes pthread_mutex_t my = PTHREAD_MUTEX_INITIALIZER Destroying: pthread_mutex_destroy(pthread_mutex_t *mutex); Locking and unlocking mutexes pthread_mutex_lock(pthread_mutex_t *mutex); pthread_mutex_trylock(pthread_mutex_t *mutex); pthread_mutex_unlock(pthread_mutex_t *mutex);

Mutex example (example2.c) int counter = 0; ptread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void *thread_func(void *arg) { int val; /* protected by mutex */ Pthread_mutex_lock( &mutex ); val = counter; counter = val + 1; Pthread_mutex_unlock( &mutex ); return NULL; } How about Making mutex a local variable?

Condition Variable Waiting and signaling on condition variables Routines pthread_cond_wait(condition, mutex) Blocks the thread until the specific condition is signalled. Should be called with mutex locked Automatically release the mutex lock while it waits When return (condition is signaled), mutex is locked again pthread_cond_signal(condition) Wake up a thread waiting on the condition variable. Called after mutex is locked, and must unlock mutex after pthread_cond_broadcast(condition) Used when multiple threads blocked in the condition

Condition Variable – for signaling Think of Producer – consumer problem Producers and consumers run in separate threads. Producer produces data and consumer consumes data. Producer has to inform the consumer when data is available Consumer has to inform producer when buffer space is available

Without Condition Variables

/* Globals */ int data_avail = 0; pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER; void *producer(void *) { Pthread_mutex_lock(&data_mutex); Produce data Insert data into queue; data_avail=1; Pthread_mutex_unlock(&data_mutex); }

void *consumer(void *) { while( !data_avail ); /* do nothing – keep looping!!*/ Pthread_mutex_lock(&data_mutex); Extract data from queue; if (queue is empty) data_avail = 0; Pthread_mutex_unlock(&data_mutex); consume_data(); }

With Condition Variables

int data_avail = 0; pthread_mutex_t data_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cont_t data_cond = PTHREAD_COND_INITIALIZER; void *producer(void *) { Pthread_mutex_lock(&data_mutex); Produce data Insert data into queue; data_avail = 1; Pthread_cond_signal(&data_cond); Pthread_mutex_unlock(&data_mutex); }

/* woken up */ void *consumer(void *) { Pthread_mutex_lock(&data_mutex); while( !data_avail ) { /* sleep on condition variable*/ Pthread_cond_wait(&data_cond, &data_mutex); } /* woken up */ Extract data from queue; if (queue is empty) data_avail = 0; Pthread_mutex_unlock(&data_mutex); consume_data();

Review What types of synchronizations are supported in Pthread? What situation can be dealt with by mutex? What situation can be dealt with by conditional variable?