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.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Chapter 6: Process Synchronization
8a-1 Programming with Shared Memory Threads Accessing shared data Critical sections ITCS4145/5145, Parallel Programming B. Wilkinson Jan 4, 2013 slides8a.ppt.
Multi-core Programming Programming with Posix Threads.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
CS533 Concepts of Operating Systems Class 3 Monitors.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
Semaphores, mutexes and condition variables. semaphores Two types – Binary – 0 or 1 – Counting 0 to n Wait – decrements > 0 forces a wait Post or signal.
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.
Pthread Synchronization Operating Systems Hebrew University Spring 2004.
Pthread II. Outline Join Mutex Variables Condition Variables.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
Multithreaded Web Server.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Atomic Operations David Monismith cs550 Operating Systems.
Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
The University of Adelaide, School of Computer Science
04/10/25Parallel and Distributed Programming1 Shared-memory Parallel Programming Taura Lab M1 Yuuki Horita.
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,
Programming with POSIX* Threads Intel Software College.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
Thread API Xiaohua Lu Office : CS 3310 Tel. : Office hours: 11-12,3-5 T,TR.
POSIX Threads HUJI Spring 2011.
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Chapter 6 P-Threads. Names The naming convention for a method/function/operation is: – pthread_thing_operation(..) – Where thing is the object used (such.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 13: Condition Variable, Read/Write Lock, and Deadlock.
Threads. Readings r Silberschatz et al : Chapter 4.
Page 1 threads CS 360, WSU Vancouver POSIX Threads 1. Background 2. Threads vs. Processes 3. Thread Synchronization 4. Mutex Variables 5. Condition Variables.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
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
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
CS 537 – Introduction to Operating Systems
Principles of Operating Systems Lecture 11
Netprog: Threads Programming
Multithreading Tutorial
Thread synchronization
Multithreading Chapter 23.
Lecture 15: Dining Philosophers Problem
Programming with Shared Memory
Lecture 14: Pthreads Mutex and Condition Variables
Multithreading.
Multithreading Tutorial
Unix System Calls and Posix Threads
Critical section problem
Multithreading Tutorial
Multithreading Tutorial
Lecture 14: Pthreads Mutex and Condition Variables
Lecture 15: Dining Philosophers Problem
Programming with Shared Memory
Programming with Shared Memory - 2 Issues with sharing data
CS 144 Advanced C++ Programming May 7 Class Meeting
“The Little Book on Semaphores” Allen B. Downey
POSIX Threads(pthreads)
Presentation transcript:

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 with it (to synchronize with its completion) is OK for some problems, but too restrictive for others. Creating a thread is relatively expensive and to minimize that overhead, it would be desirable to create a set of threads once, keeping them in a pool to be allocated to tasks. 4 Sort of like the OS does when allocating processors to processes

Page 3 CS 360, WSU Vancouver Pthread’s Condition Variables Not really a variable 4 It does not contain your program’s data Rather a means of allowing threads to: 4 Wait to be signaled (woken up) by another thread 4 Signal other waiting threads (no relation to UNIX signal() or kill() system facilities) What you make your threads wait for, and when you signal them is all up to you.

Page 4 CS 360, WSU Vancouver More… New pthread type: 4 pthread_cond_t 4 Think of it as a list of threads waiting to be signaled (woken up) Declare it as a global variable, or allocate it on the heap 4 It has to be shareable by all threads! Initialize it before use: pthread_cond_init(pthread_cond_t* someConditionVar);

Page 5 CS 360, WSU Vancouver Mutexes In most cases, you will need to protect access to a condition variable (and the shared data associated with it) with a pthread_mutex Your shared data that is being examined and your condition variable should not be viewed or modified by a thread unless it has exclusive access to it, hence get a lock on it…

Page 6 CS 360, WSU Vancouver Waiting for a condition… The following procedure: pthread_cond_wait(pthread_cond_t* myCond, pthread_mutex_t* mutex) Adds the calling thread to the list of threads waiting for “myCond” and suspends the calling thread until “myCond” is signaled. The associated mutex should already be locked by the calling thread. “myCond” is modified to add the calling thread, then the mutex is unlocked and the thread suspended. When (or if) the calling thread returns from pthread_cond_wait the mutex will have been locked for the thread.

Page 7 CS 360, WSU Vancouver Signaling a condition… The following procedure: pthread_cond_signal(pthread_cond_t* myCond); This procedure “wakes” up threads that are suspended waiting for “myCond”. Note that if multiple threads are waiting, only one will be able to lock its mutex and execute. The selection of which one gets the lock (after the calling thread gives it up) and continues execution is arbitrary. Note that no mutex variable is passed to the procedure, however, the calling thread must have exclusive access to “myCond” when calling this procedure. Therefore, you should lock the associate mutex variable before calling it (and unlock it afterwards, as your program logic requires). The calling thread is not suspended by calling pthread_cond_signal

Page 8 CS 360, WSU Vancouver Example The following code is executed by one or more threads which “produce” a object and store its address in global variable “task”. The object cannot be produced for consumption unless the global variable “task” is NULL first. Other threads seek to “consume” such objects, and may be waiting to be notified via a condition variable. /* global shared stuff, assume it is initialized*/ TaskData* task; pthread_cond_t wantTask; pthread_cond_t spaceForTask; pthread_mutex_t mutex;

Page 9 CS 360, WSU Vancouver Producer while (more tasks to produce) { /* do the work, producing a task for the consumers */ Task* temp = produceTask(); /* lock up the shared variable items */ pthread_mutex_lock(&mutex);/* lock up the data */ if (task != NULL) { /* wait until informed task variable is NULL */ pthread_cond_wait(&spaceForTask,&mutex); } assert(task == NULL); task = temp;/* post the task /* signal the consumers a task is ready */ pthread_cond_signal(&wantTask); pthread_mutex_unlock(&mutex);/* give up the lock */ }

Page 10 CS 360, WSU Vancouver Consumer while (1) { Task* temp = NULL; /* get a lock on the shared variables… */ pthread_mutex_lock(&mutex);/* get a lock on the data */ if (task == NULL) {/* wait for a task */ pthread_cond_wait(&wantTask,&mutex); } if (task != NULL) { temp = task;/* found a task, take it */ task = NULL; /* let the producers know there is room for more */ pthread_cond_signal(&spaceForTask); } pthread_mutex_unlock(&mutex);/* give up the lock */ if (temp) consumeTask(temp);/* do the work, consume task */ }