Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo. Daniele Loiacono

Similar documents
Process Synchronization

Pthreads: POSIX Threads

Agenda. Process vs Thread. ! POSIX Threads Programming. Picture source:

Multi-threaded Programming

POSIX Threads Programming

Threads need to synchronize their activities to effectively interact. This includes:

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

POSIX Threads Programming

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

ANSI/IEEE POSIX Standard Thread management

Parallel Computing: Programming Shared Address Space Platforms (Pthread) Jin, Hai

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

POSIX threads CS 241. February 17, Copyright University of Illinois CS 241 Staff

POSIX Threads. HUJI Spring 2011

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Synchronization Primitives

Unix. Threads & Concurrency. Erick Fredj Computer Science The Jerusalem College of Technology

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

CS240: Programming in C. Lecture 18: PThreads

Лаб 8. POSIX threads.

CSC 447: Parallel Programming for Multi-Core and Cluster Systems

TCSS 422: OPERATING SYSTEMS

Oct 2 and 4, 2006 Lecture 8: Threads, Contd

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

Threads. Jo, Heeseung

HPCSE - I. «Introduction to multithreading» Panos Hadjidoukas

Paralleland Distributed Programming. Concurrency

Preview. What are Pthreads? The Thread ID. The Thread ID. The thread Creation. The thread Creation 10/25/2017

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Multithreading Programming II

Introduction to PThreads and Basic Synchronization

CSCI4430 Data Communication and Computer Networks. Pthread Programming. ZHANG, Mi Jan. 26, 2017

POSIX PTHREADS PROGRAMMING

Thread Fundamentals. CSCI 315 Operating Systems Design 1

THREADS. Jo, Heeseung

Introduction to parallel computing

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

More Shared Memory Programming

CS 220: Introduction to Parallel Computing. Condition Variables. Lecture 24

Introduc)on to pthreads. Shared memory Parallel Programming

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CSE 333 SECTION 9. Threads

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Threads. Jo, Heeseung

CS333 Intro to Operating Systems. Jonathan Walpole

Lecture 19: Shared Memory & Synchronization

Posix Threads (Pthreads)

CS510 Operating System Foundations. Jonathan Walpole

Operating systems and concurrency (B08)

Shared Memory Programming Models III

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

User Space Multithreading. Computer Science, University of Warwick

Chapter 5: Achieving Good Performance

Programming with Shared Memory

CS Operating Systems: Threads (SGG 4)

Operating Systems (2INC0) 2017/18

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

Embedded Multicore Building Blocks (EMB²)

CS 5523 Operating Systems: Thread and Implementation

High Performance Computing Course Notes Shared Memory Parallel Programming

Shared Memory Programming. Parallel Programming Overview

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

Condition Variables. Dongkun Shin, SKKU

A Brief Introduction to OS/2 Multithreading

POSIX Threads. Paolo Burgio

Computer Systems Laboratory Sungkyunkwan University

20-EECE-4029 Operating Systems Fall, 2015 John Franco

CS 153 Lab6. Kishore Kumar Pusukuri

CS 167 Midterm Exam. Two Hours; Closed Book; Please Use a Black Pen March 23, 2017

CS533 Concepts of Operating Systems. Jonathan Walpole

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Process Synchronization

PThreads in a Nutshell

CS 6400 Lecture 11 Name:

CSCI 4061: Threads in a Nutshell

CS 3305 Intro to Threads. Lecture 6

Condition Variables. Dongkun Shin, SKKU

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Program threaded-fft-mutex.cc

CSE 333 Section 9 - pthreads

real time operating systems course

A Programmer s View of Shared and Distributed Memory Architectures

Concurrency and Synchronization. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

CS510 Operating System Foundations. Jonathan Walpole

Program threaded-fft-bakery.cc

Threads (SGG 4) Outline. Traditional Process: Single Activity. Example: A Text Editor with Multi-Activity. Instructor: Dr.

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

TCSS 422: OPERATING SYSTEMS

LSN 13 Linux Concurrency Mechanisms

Synchronization. Semaphores implementation

Reading compiler errors

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

Chapter 4 Concurrent Programming

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

PRINCIPLES OF OPERATING SYSTEMS

Mixed-Mode Programming

Transcription:

Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo

References The material in this set of slide is taken from two tutorials by Blaise Barney from the Lawrence Livermore National Laboratory Introduction to Parallel Computing Blaise Barney, Lawrence Livermore National Laboratory https://computing.llnl.gov/tutorials/parallel_comp/ Also available as Dr.Dobb s Go Parallel Introduction to Parallel Computing: Part 2 Blaise Barney, Lawrence Livermore National Laboratory

Overview Condition variables provide yet another way for threads to synchronize. While mutexes implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data. Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), 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.

Mutexes vs Condition Variables Mutexes and Condition Variables are way for threads to synchronize Mutexes implement synchronization by controlling thread access to data Condition Variables allow threads to synchronize based upon the actual value of data. Without condition variables, threads continually poll 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.

Typical Scenario Declare and initialize global data/variables Delare and initialize a condition variable object Declare and initialize an associated mutex Create threads A and B to do work Do work Lock associated mutex and check of a global variable If value does not meet some condition, perform a blocking wait (automatically and atomically unlocks the associated mutex) When signalled, wake up (mutex is automatically and atomically locked) Explicitly unlock mutex Continue Join / Continue A Main Do work Lock mutex Change the value of the global variable that Thread-A is waiting upon If the new value met the condition desired by Thread- A, signal it tothread-a Unlock mutex Continue B Main

Declaration and initialization Condition variables must be declared with type pthread_cond_t There are two ways to initialize a condition variable: Statically, when it is declared. For example: pthread_cond_t myconvar=pthread_cond_initializer; Dynamically, with the pthread_cond_init(condition,attr)routine ID of the created condition variable is returned through condition attr if not NULL, permits setting condition variable object attributes pthread_cond_destroy() should be used to free a condition variable that is no longer needed.

Waiting and Signaling on Condition Variables pthread_cond_wait (condition,mutex) pthread_cond_signal (condition) pthread_cond_broadcast (condition) pthread_cond_wait() blocks the calling thread until the specified condition is signalled. pthread_cond_signal() routine is used to signal (or wake up) another thread which is waiting on the condition variable. pthread_cond_broadcast() routine should be used instead of pthread_cond_signal() if more than one thread is in a blocking wait state. It is a logical error to call pthread_cond_signal() before calling pthread_cond_wait()

Waiting and Signaling on Condition Variables (2) pthread_cond_wait() should be called while mutex is locked and it will automatically release the mutex while it waits when wakes up, mutex will be automatically locked for use by the thread programmer is responsible for unlocking mutex when the thread is finished with it pthread_cond_signal() should be called after mutex is locked must unlock mutex in order for pthread_cond_wait() routine to complete

Example The main routine creates three threads. Two of the threads perform work and update a "count" variable. The third thread waits until the count variable reaches a specified value #define NUM_THREADS 3 #define TCOUNT 10 #define COUNT_LIMIT 12 int count = 0; int thread_ids[3] = {0,1,2; pthread_mutex_t count_mutex; pthread_cond_t count_threshold_cv; void *inc_count(void *t){ void *watch_count(void *t){ int main (int argc, char *argv[]){

Example (2) int main (int argc, char *argv[]){ int I; long t1=1, t2=2, t3=3; pthread_t threads[3]; pthread_attr_t attr; pthread_mutex_init(&count_mutex, NULL); pthread_cond_init (&count_threshold_cv, NULL); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_create(&threads[0], &attr, watch_count, (void *)t1); pthread_create(&threads[1], &attr, inc_count, (void *)t2); pthread_create(&threads[2], &attr, inc_count, (void *)t3); for (i=0; i<num_threads; i++) { /* Wait for all threads */ pthread_join(threads[i], NULL); pthread_attr_destroy(&attr); pthread_mutex_destroy(&count_mutex); pthread_cond_destroy(&count_threshold_cv); pthread_exit(null);

Example (3) void *inc_count(void *t){ long my_id = (long)t; for (int i=0; i<tcount; i++) { pthread_mutex_lock(&count_mutex); count++; if (count == COUNT_LIMIT) { pthread_cond_signal(&count_threshold_cv); pthread_mutex_unlock(&count_mutex); sleep(1); pthread_exit(null); void *watch_count(void *t){ long my_id = (long)t; pthread_mutex_lock(&count_mutex); if (count<count_limit) { pthread_cond_wait(&count_threshold_cv, &count_mutex); count += 125; pthread_mutex_unlock(&count_mutex); pthread_exit(null);