Introduction to C Threads Instructor Yin Lou 02 16 2011 Introduction to C CS 2022 Spring 2011 Lecture 11 Processes vs Threads Processes Threads I Multiple simultaneous programs I Multiple simultaneous functions I Independent memory space I Shared memory space I Independent open file descriptors I Shared open file descriptors Introduction to C CS 2022 Spring 2011 Lecture 11 Threads Examples I Graphical User Interfaces GUIs I I I The GUI is usually put on a separate thread from the app engine GUI remains responsive even if app blocks for processing Web Browser Tabs I I I Each tab is managed by a separate thread for rendering Web pages render simultaneously Note Google Chrome actually uses a separate process per tab Introduction to C CS 2022 Spring 2011 Lecture 11 Threads I One copy of the heap I One copy of the code I Multiple stacks Introduction to C CS 2022 Spring 2011 Lecture 11 Threads I One copy of the heap I One copy of the code I Multiple stacks I Life Cycle Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I include pthread h Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I I include pthread h Define a worker function Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I I include pthread h Define a worker function I void foo void args Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I I include pthread h Define a worker function I I void foo void args Initialize pthread attr t Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I I include pthread h Define a worker function I I void foo void args Initialize pthread attr t I I pthread attr t attr pthread attr init attr Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I I include pthread h Define a worker function I I Initialize pthread attr t I I I void foo void args pthread attr t attr pthread attr init attr Create a thread Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I I include pthread h Define a worker function I I Initialize pthread attr t I I I void foo void args pthread attr t attr pthread attr init attr Create a thread I I pthread t thread pthread create thread attr worker function arg Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I I include pthread h Define a worker function I I Initialize pthread attr t I I I pthread attr t attr pthread attr init attr Create a thread I I I void foo void args pthread t thread pthread create thread attr worker function arg Exit current thread Introduction to C CS 2022 Spring 2011 Lecture 11 pthread I I include pthread h Define a worker function I I Initialize pthread attr t I I I pthread attr t attr pthread attr init attr Create a thread I I I void foo void args pthread t thread pthread create thread attr worker function arg Exit current thread I pthread exit status Introduction to C CS 2022 Spring 2011 Lecture 11 Example include stdio h include pthread h define NUM THREADS 5 void print hello void threadid long tid tid long threadid printf Hello World It s me thread ld n tid pthread exit NULL int main int argc char argv pthread t threads NUM THREADS int rc long t for t 0 t NUM THREADS t printf In main creating thread ld n t rc pthread create threads t NULL print hello void t if rc printf ERROR return code from pthread create is d n rc exit 1 pthread exit NULL Introduction to C CS 2022 Spring 2011 Lecture 11 Thread Management I pthread join threadid status I pthread detach threadid Introduction to C CS 2022 Spring 2011 Lecture 11 Example include stdio h include pthread h define NUM THREADS 5 void print hello void threadid long tid tid long threadid printf Hello World It s me thread ld n tid pthread exit NULL int main int argc char argv pthread t threads NUM THREADS int rc long t for t 0 t NUM THREADS t printf In main creating thread ld n t rc pthread create threads t NULL print hello void t if rc printf ERROR return code from pthread create is d n rc exit 1 wait for all threads to complete for t 0 t NUM THREADS t pthread join threads t NULL pthread exit NULL Introduction to C CS 2022 Spring 2011 Lecture 11 Compiling I Use pthread Introduction to C CS 2022 Spring 2011 Lecture 11 Compiling I Use pthread Makefile CC gcc OPTIONS O2 LIB PATH pthread SRC DIR src DST DIR bin default CC OPTIONS LIB PATH SRC DIR c o DST DIR test clean cd DST DIR rm test Introduction to C CS 2022 Spring 2011 Lecture 11
View Full Document
Unlocking...