Programming with POSIX* Threads Intel Software College.

Slides:



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

Threads Programming Thread creation Synchronization.
Programming with Posix Threads CS5204 Operating Systems.
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
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
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.
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.
Pthread II. Outline Join Mutex Variables Condition Variables.
Comp 422: Parallel Programming Shared Memory Multithreading: Pthreads Synchronization.
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.
Thread-Safe Programming Living With Linux. Thread-Safe Programming Tommy Reynolds Fedora Documentation Project Steering Committee
04/10/25Parallel and Distributed Programming1 Shared-memory Parallel Programming Taura Lab M1 Yuuki Horita.
June-Hyun, Moon Computer Communications LAB., Kwangwoon University Chapter 26 - Threads.
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.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
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
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.
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.
POSIX Threads HUJI Spring 2011.
Lecture 7: POSIX Threads - Pthreads. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
IT 325 Operating systems Chapter6.  Threads can greatly simplify writing elegant and efficient programs.  However, there are problems when multiple.
Pthreads.
Threads Tutorial #7 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers – It is.
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.
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.
PThread Synchronization. Thread Mechanisms Birrell identifies four mechanisms commonly used in threading systems –Thread creation –Mutual exclusion (mutex)
Thread S04, Recitation, Section A Thread Memory Model Thread Interfaces (System Calls) Thread Safety (Pitfalls of Using Thread) Racing Semaphore.
Thread Basic Thread operations include thread creation, termination, synchronization, data management Threads in the same process share:  Process address.
POSIX THREADS. What is a thread? Multiple stands of execution in a single program are called threads. In other words, a thread is a sequence of control.
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
回到第一頁 What are threads n Threads are often called "lightweight processes” n In the UNIX environment a thread: u Exists within a process and uses the process.
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Synchronization and Semaphores
PThreads.
Principles of Operating Systems Lecture 11
Programming with POSIX* Threads
Shared-Memory Programming with Threads
Threads Threads.
Netprog: Threads Programming
Copyright ©: Nahrstedt, Angrave, Abdelzaher
PTHREADS These notes are from LLNL Pthreads Tutorial
Multithreading Tutorial
Thread synchronization
Operating Systems Lecture 13.
Lecture 14: Pthreads Mutex and Condition Variables
PTHREADS AND SEMAPHORES
Multithreading Tutorial
Jonathan Walpole Computer Science Portland State University
Pthread Prof. Ikjun Yeom TA – Mugyo
Multithreading Tutorial
Multithreading Tutorial
Lecture 14: Pthreads Mutex and Condition Variables
CS 144 Advanced C++ Programming May 7 Class Meeting
POSIX Threads(pthreads)
Presentation transcript:

Programming with POSIX* Threads Intel Software College

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 2 Programming with POSIX* Threads Objectives Explore Pthreads “core” functions to create and synchronize threads

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 3 Programming with POSIX* Threads What is Pthreads? POSIX.1c standard C language interface Threads exist within same process All threads are peers No explicit parent-child model Exception: “main thread” holds process information

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 4 Programming with POSIX* Threads pthread_create int pthread_create(tid, attr, function, arg); pthread_t *tid handle of created thread const pthread_attr_t *attr attributes of thread to be created void *(*function)(void *) function to be mapped to thread void *arg single argument to function

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 5 Programming with POSIX* Threads pthread_create Explained Spawn a thread running the function Thread handle returned via pthread_t structure Specify NULL to use default attributes Single argument sent to function If no arguments to function, specify NULL Check error codes! EAGAIN - insufficient resources to create thread EINVAL - invalid attribute

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 6 Programming with POSIX* Threads Example: Thread Creation #include void *hello (void * arg) { printf(“Hello Thread\n”); } main() { pthread_t tid; pthread_create(&tid, NULL, hello, NULL); } What Happens?

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 7 Programming with POSIX* Threads Waiting for a Thread int pthread_join(tid, val_ptr); pthread_t tid handle of joinable thread void **val_ptr exit value returned by joined thread

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 8 Programming with POSIX* Threads pthread_join Explained Calling thread waits for thread with handle tid to terminate Only one thread can be joined Thread must be joinable Exit value is returned from joined thread Type returned is (void *) Use NULL if no return value expected ESRCH - thread (pthread_t) not found EINVAL - thread (pthread_t) not joinable

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 9 Programming with POSIX* Threads Thread States Pthreads threads have two states joinable and detached Threads are joinable by default Resources are kept until pthread_join Can be reset with attributes or API call Detached threads cannot be joined Resources can be reclaimed at termination Cannot reset to be joinable

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 10 Programming with POSIX* Threads Example: Multiple Threads #include #define NUM_THREADS 4 void *hello (void *arg) { printf(“Hello Thread\n”); } main() { pthread_t tid[NUM_THREADS]; for (int i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i], NULL, hello, NULL); for (int i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 11 Programming with POSIX* Threads What’s Wrong? What is printed for myNum? void *threadFunc(void *) { void *threadFunc(void *pArg) { int* p = (int*)pArg; int* p = (int*)pArg; int myNum = *p; printf( “Thread number %d\n”, myNum);}... // from main(): for (int i = 0; i < numThreads; i++) { pthread_create(&tid[i], NULL, threadFunc, &i); pthread_create(&tid[i], NULL, threadFunc, &i);}

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 12 Programming with POSIX* Threads Solution – “Local” Storage void *threadFunc(void *) void *threadFunc(void *pArg){ (int*)pArg) int myNum = *((int*)pArg); printf( “Thread number %d\n”, myNum);}... // from main(): for (int i = 0; i < numThreads; i++) { tNum[i] = i; tNum[i] = i; pthread_create(&tid[i], NULL, threadFunc, &tNum[i]); pthread_create(&tid[i], NULL, threadFunc, &tNum[i]);}

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 13 Programming with POSIX* Threads Pthreads Mutex Variables Simple, flexible, and efficient Enables correct programming structures for avoiding race conditions New types pthread_mutex_t the mutex variable pthread_mutexattr_t mutex attributes Before use, mutex must be initialized

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 14 Programming with POSIX* Threads pthread_mutex_init int pthread_mutex_init( mutex, attr ); pthread_mutex_t *mutex mutex to be initialized const pthread_mutexattr_t *attr attributes to be given to mutex ENOMEM - insufficient memory for mutex EAGAIN - insufficient resources (other than memory) EPERM - no privilege to perform operation

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 15 Programming with POSIX* Threads Alternate Initialization Can also use the static initializer PTHREAD_MUTEX_INITIALIZER Uses default attributes Programmer must always pay attention to mutex scope Must be visible to threads pthread_mutex_t mtx1 = PTHREAD_MUTEX_INITIALIZER;

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 16 Programming with POSIX* Threads pthread_mutex_lock int pthread_mutex_lock( mutex ); pthread_mutex_t *mutex mutex to attempt to lock

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 17 Programming with POSIX* Threads pthread_mutex_lock Explained Attempts to lock mutex If mutex is locked by another thread, calling thread is blocked Mutex is held by calling thread until unlocked Mutex lock/unlock must be paired or deadlock occurs EINVAL - mutex is invalid EDEADLK - calling thread already owns mutex

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 18 Programming with POSIX* Threads pthread_mutex_unlock int pthread_mutex_unlock( mutex ); pthread_mutex_t *mutex mutex to be unlocked EINVAL - mutex is invalid EPERM - calling thread does not own mutex

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 19 Programming with POSIX* Threads Example: Use of mutex #define NUMTHREADS 4 pthread_mutex_t gMutex; // why does this have to be global? int g_sum = 0; void *threadFunc(void *arg) { int mySum = bigComputation(); pthread_mutex_lock( &gMutex ); g_sum += mySum; // threads access one at a time pthread_mutex_unlock( &gMutex ); } main() { pthread_t hThread[NUMTHREADS]; pthread_mutex_init( &gMutex, NULL ); for (int i = 0; i < NUMTHREADS; i++) pthread_create(&hThread[i],NULL,threadFunc,NULL); for (int i = 0; i < NUMTHREADS; i++) pthread_join(hThread[i]); printf (“Global sum = %f\n”, g_sum); }

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 20 Programming with POSIX* Threads Condition Variables Semaphores are conditional on the semaphore count Condition variable is associated with an arbitrary conditional Same operations: wait and signal Provides mutual exclusion

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 21 Programming with POSIX* Threads Condition Variable and Mutex Mutex is associated with condition variable Protects evaluation of the conditional expression Prevents race condition between signaling thread and threads waiting on condition variable

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 22 Programming with POSIX* Threads Lost and Spurious Signals Signal to condition variable is not saved If no thread waiting, signal is “lost” Thread can be deadlocked waiting for signal that will not be sent Condition variable can (rarely) receive spurious signals Slowed execution from predictable signals Need to retest conditional expression

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 23 Programming with POSIX* Threads Condition Variable Algorithm Avoids problems with lost and spurious signals acquire mutex; while (conditional is true) wait on condition variable; wait on condition variable; perform critical region computation; update conditional; signal sleeping thread(s); release mutex; Negation of condition needed to proceed Mutex is automatically released when thread waits May be optional

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 24 Programming with POSIX* Threads Condition Variables pthread_cond_init, pthread_cond_destroy initialize/destroy condition variable pthread_cond_wait thread goes to sleep until signal of condition variable pthread_cond_signal signal release of condition variable pthread_cond_broadcast broadcast release of condition variable

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 25 Programming with POSIX* Threads Condition Variable Types Data types used pthread_cond_t the condition variable pthread_condattr_t condition variable attributes Before use, condition variable (and mutex) must be initialized

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 26 Programming with POSIX* Threads pthread_cond_init int pthread_cond_init( cond, attr ); pthread_cond_t *cond condition variable to be initialized const pthread_condattr_t *attr attributes to be given to condition variable ENOMEM - insufficient memory for condition variable EAGAIN - insufficient resources (other than memory) EBUSY - condition variable already intialized EINVAL - attr is invalid

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 27 Programming with POSIX* Threads Alternate Initialization Can also use the static initializer PTHREAD_COND_INITIALIZER Uses default attributes Programmer must always pay attention to condition (and mutex) scope Must be visible to threads pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 28 Programming with POSIX* Threads pthread_cond_wait int pthread_cond_wait( cond, mutex ); pthread_cond_t *cond condition variable to wait on pthread_mutex_t *mutex mutex to be unlocked

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 29 Programming with POSIX* Threads pthread_cond_wait Explained Thread put to “sleep” waiting for signal on cond Mutex is unlocked Allows other threads to acquire lock When signal arrives, mutex will be reacquired before pthread_cond_wait returns EINVAL - cond or mutex is invalid EINVAL - different mutex for concurrent waits EINVAL - calling thread does not own mutex

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 30 Programming with POSIX* Threads pthread_cond_signal int pthread_cond_signal( cond ); pthread_cond_t *cond condition variable to be signaled

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 31 Programming with POSIX* Threads pthread_cond_signal Explained Signal condition variable, wake one waiting thread If no threads waiting, no action taken Signal is not saved for future threads Signaling thread need not have mutex May be more efficient Problem may occur if thread priorities used EINVAL - cond is invalid

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 32 Programming with POSIX* Threads pthread_cond_broadcast int pthread_cond_broadcast( cond ); pthread_cond_t *cond condition variable to signal

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 33 Programming with POSIX* Threads pthread_cond_broadcast Explained Wake all threads waiting on condition variable If no threads waiting, no action taken Broadcast is not saved for future threads Signaling thread need not have mutex EINVAL - cond is invalid

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 34 Programming with POSIX* Threads Programming with POSIX* Threads What’s Been Covered How to create threads to execute work encapsulated within functions Coordinate shared access between threads to avoid race conditions

Copyright © 2006, Intel Corporation. All rights reserved. Intel and the Intel logo are trademarks or registered trademarks of Intel Corporation or its subsidiaries in the United States or other countries. *Other brands and names are the property of their respective owners. 35 Programming with POSIX* Threads