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.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

CS Lecture 4 Programming with Posix Threads and Java Threads George Mason University Fall 2009.
Threads Programming Thread creation Synchronization.
Programming with Posix Threads CS5204 Operating Systems.
Multi-core Programming Programming with Posix Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Threads Lab اللهم علمنا ما ينفعنا،،، وانفعنا بما علمتنا،،، وزدنا علماً
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)
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.
POSIX Threads HUJI Spring 2007.
Pthread II. Outline Join Mutex Variables Condition Variables.
Comp 422: Parallel Programming Shared Memory Multithreading: Pthreads Synchronization.
Condition Variables Revisited Copyright ©: University of Illinois CS 241 Staff1.
Netprog Threads Programming1 Threads Programming Refs: Chapter 23.
THREAD IMPLEMENTATION For parallel processing. Steps involved Creation Creates a thread with a thread id. Detach and Join All threads must be detached.
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.
Atomic Operations David Monismith cs550 Operating Systems.
The University of Adelaide, School of Computer Science
Thread-Safe Programming Living With Linux. Thread-Safe Programming Tommy Reynolds Fedora Documentation Project Steering Committee
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
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,
Programming with POSIX* Threads Intel Software College.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Pthreads: A shared memory programming model
1 Pthread Programming CIS450 Winter 2003 Professor Jinhua Guo.
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.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
Pthreads.
Pthreads #include pthread_t tid ; //thread id. pthread_attr_t attr ; void *sleeping(void *); /* thread routine */ main() { int time = 2 ; pthread_create(&tid,
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.
Unix Internals Concurrent Programming. Unix Processes Processes contain information about program resources and program execution state, including: Process.
Threads A thread is an alternative model of program execution
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.
1 Reading compiler errors ls2.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token In file included from /usr/include/stdio.h:75,
Synchronization and Semaphores
CS 537 – Introduction to Operating Systems
Lecture 5 : Pthread Programming
Principles of Operating Systems Lecture 11
Threads in C Caryl Rahn.
Shared-Memory Programming with Threads
Threads Threads.
Netprog: Threads Programming
Chapter 4: Threads.
PTHREADS These notes are from LLNL Pthreads Tutorial
Lecture 14: Pthreads Mutex and Condition Variables
PTHREADS AND SEMAPHORES
Pthread Prof. Ikjun Yeom TA – Mugyo
Lecture 14: Pthreads Mutex and Condition Variables
Programming with Shared Memory - 2 Issues with sharing data
POSIX Threads(pthreads)
Presentation transcript:

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 with a mutex lock  The mutex provides the mutual exclusive aspect of a monitor u The attributes for condition variables must be set and initialized before the condition variables can be used

Steps in using a condition variable u Create an attribute object u Create a condition variable, associating it with an existing mutex u Use the condition variable  wait, signal, broadcast u Destroy the condition variable

Attribute Initialization int threads_condattr_init( pthread_condattr_t *cattr);  The pthread_condattr_init() call returns a pointer to an opaque object  The possible values of cattr’s scope are PTHREAD_PROCESS_PRIVATE (the default) and PTHREAD_PROCESS_SHARED u If the object is not destroyed, a memory leak will result

Setting The Scope int pthread_condattr_setpshared( pthread_condattr_t *cattr, int pshared);  pthread_condattr_setpshared( ) sets the scope of a condition variable to either process private(intraprocess) or system wide (interprocess)  pshared = PTHREAD_PROCESS_SHARED can be shared among threads from more than one process  pshared = PTHREAD_PROCESS_PRIVATE only threads in the same process can operate on the object

Attribute Destruction int pthread_condattr_destroy( pthread_condattr_t *cattr);  Use pthread_condattr_destroy( ) to remove storage and render the object invalid u The object must be reinitialized before it can be reused

Initialize A Condition Variable int pthread_cond_init( pthread_cond_t *cv, const pthread_condattr_t *cattr);  Initializes the condition variable pointed at by cv to its default value (cattr = NULL)  specify condition variable attributes that are already set with pthread_condattr_init()

Wait On Condition Variable int pthread_cond_wait( pthread_cond_t *cv, pthread_mutex_t *mp);  Use pthread_cond_wait( ) to release the mutex pointed to by mp and to cause the calling thread to block on the condition variable pointed to by cv  The blocked thread can be awakened by pthread_cond_signal() pthread_cond_broadcast()

Unblock A Thread int pthread_cond_signal( pthread_cond_t *cv);  Use pthread_cond_signal( ) to unblock one thread that is blocked on the condition variable pointed to by cv  If more than one thread is blocked on a condition variable, the scheduling policy determines the order in which blocked threads are awakened.  For SCHED_OTHER, threads are awakened in priority order

Unblock All Threads Int pthread_cond_broadcast( pthread_cond_t *cv);  Use pthread_cond_broadcast() to unblock all threads that are blocked on the condition variable pointed to by cv, specified by pthread_cond_wait()  When no threads are blocked on the condition variable, pthread_cond_broadcast() has no effect

Destroy Condition Variable int pthread_cond_destroy( pthread_cond_t *cv); u The pthread_cond_destroy() function destroys a previously initialized condition variable u The condition variable must not be used after it has been destroyed. u The space for storing the condition variable is not freed.

condvar.c #include using namespace std; const int t = 5; const int MAX = 2000; const int MAX_COUNT = t*MAX; int counter = 0; pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t count_max = PTHREAD_COND_INITIALIZER; int thread_id[t+1];

condvar.c (continued) void* watch(void* ID) { int* id = (int*)ID; pthread_mutex_lock(&count_mutex); cout << "The watch has begun." << endl; while(counter < MAX_COUNT) pthread_cond_wait(&count_max,&count_mutex); cout << "The value is "<< counter<< "######"<< endl; pthread_mutex_unlock(&count_mutex); return NULL; }

condvar.c (continued) void* increment(void* ID) { int *id = (int*)ID; int i; for(i=0; i< MAX ; i++) { pthread_mutex_lock(&count_mutex); counter++; cout<<"id: "<<*id<<" i: "<<i<<" counter: " <<counter<< endl; if (counter == MAX_COUNT) pthread_cond_signal(&count_max); pthread_mutex_unlock(&count_mutex); } return NULL; }

condvar.c int main(int argc, char *argv[]) { pthread_attr_t myATTR; pthread_attr_init (&myATTR); pthread_attr_setscope(&myATTR, PTHREAD_SCOPE_SYSTEM); for(int i=0;i<t+1;i++) thread_id[i]=i; pthread_t thread[t+1]; pthread_create(&thread[t], &myATTR, watch, (void*)&thread_id[t]); for(int i=0; i<t;i++) pthread_create(&thread[i], &myATTR, increment, (void*)&thread_id[i]); for(int i=0; i<= t ; i++) pthread_join(thread[i], NULL); return 0; }

Compiling your C++ code with pthreads u You should use the g++ form of complier invocation u You must link with the pthread library  g++ mycode.cpp -lpthread u If you don’t include the link option, only your main thread will execute! u This works with Solaris; details vary from system to system