Unformatted text preview:

Threads CS 2022 Introduction to C Instructor Hussam Abu Libdeh Cornell University based on slides by Saikat Guha Fall 2009 Lecture 11 Threads CS 2022 Fall 2009 Lecture 11 Processes vs Threads Processes I Multiple simultaneous programs I Independent memory space I Independent open file descriptors Threads Threads I Multiple simultaneous functions I Share the same memory I Share the same open file descriptors CS 2022 Fall 2009 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 Threads CS 2022 Fall 2009 Lecture 11 Threads I I I One copy of the heap One copy of the code Multiple stacks Threads CS 2022 Fall 2009 Lecture 11 Threads include pthread h void main2 void arg void main1 void arg int main pthread t id1 id2 pthread create id1 NULL main1 NULL pthread create id2 NULL main2 NULL think multiple processors or cores Threads CS 2022 Fall 2009 Lecture 11 pthread Starting a thread include pthread h pthread t id err pthread create id NULL entry func arg Body of a thread void entry func void arg Exiting current thread pthread exit void return value Threads CS 2022 Fall 2009 Lecture 11 pthread Co operative Multi Threading on Single Processor include sched h sched yield I I I Store stack pointer internal state etc for current thread Restore stack pointer internal state etc for another thread Resume executing other thread From the caller s perspective sched yield blocks until the other thread calls sched yield Allows multiple threads to share the CPU cooperatively Threads CS 2022 Fall 2009 Lecture 11 pthread Non Cooperative Multi Threading Thread library pthread pre empts thread when it invokes an OS function I I Almost transparent when writing code Whole new class of bugs Concurrency bugs I I I Multiple threads accessing same object concurrently Solution Locks only one thread can grab lock More of this is CS414 415 Threads CS 2022 Fall 2009 Lecture 11


View Full Document

CORNELL CS 2022 - Threads

Loading Unlocking...
Login

Join to view Threads and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Threads and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?