Realizing Concurrency using the thread modelModels of concurrencyIntroductionTopics to be CoveredObjectivesPer process vs per thread itemsThread as a unit of workPthread LibraryPosix Library Implementation in F. Mueller’s PaperCreating threadsUsing threadsThread’s local dataThread termination (destruction)Waiting for thread exitThe Thread ModelImplementing Threads in User SpaceImplementing Threads in the KernelHybrid ImplementationsScheduler ActivationsPop-Up ThreadsThread Scheduling (1)Thread Scheduling (2)SummaryB. RAMAMURTHY01/14/191Realizing Concurrency using the thread modelModels of concurrencyThere are two prevalent models of concurrency in most systemsThe process model (heavy weight: provides complete isolation; separate address space/process) The thread model (light weight: many operate in the same address space)We will discuss the thread model as defined by Posix thread now. We will discuss the process model later.01/14/192Introduction01/14/193A thread refers to a thread of control flow: an independent sequence of execution of program code.Threads are powerful. As with most powerful tools, if they are not used appropriately thread programming may be inefficient.Thread programming has become viable solution for many problems with the advent of multi-core processorsTypically these problems are expected to handle many requests simultaneously. Example: multi-media, games, automotive embedded systemsEspecially relevant to embedded system with the proliferation of multi-core processorsTopics to be Covered01/14/194ObjectivesWhat are Threads?Thread implementation modelsPOSIX threadsCreating threadsUsing threadsSummaryObjectives01/14/195To understand the thread model for realizing concurrencyTo study POSIX standard for threads called the Pthreads.To study thread control primitives for creation, termination, join, synchronization, concurrency, and scheduling.To learn to design multi-threaded applications.Per process vs per thread items01/14/196Items shared by all threads in a processItems private to each threadThread as a unit of work01/14/197A thread is a unit of work to a CPU. It is strand of control flow.A traditional UNIX process has a single thread that has sole possession of the process’s memory and resources.Threads within a process are scheduled and execute independently.Many threads may share the same address space.Each thread has its own private attributes: stack, program counter and register context.Pthread Library01/14/198Many thread models emerged: Solaris threads, win-32 threadsA POSIX standard (IEEE 1003.1c) API for thread creation and synchronization.API specifies behavior of the thread library, implementation is up to development of the library.Simply a collection of C functions.Posix Library Implementationin F. Mueller’s Paper01/14/199Language ApplicationLanguage Interface C Language ApplicationPosix thread libraryUnix KernelUnix librariesUser LevelKernel LevelCreating threads01/14/1910Always include pthread library: #include <pthread.h>int pthread_create (pthread_t *tp, const pthread_attr_t * attr, void *(* start_routine)(void *), void *arg);This creates a new thread of control that calls the function start_routine.It returns a zero if the creation is successful, and thread id in tp (first parameter).attr is to modify the attributes of the new thread. If it is NULL default attributes are used.The arg is passing arguments to the thread function.Using threads01/14/19111. Declare a variable of type pthread_t2. Define a function to be executed by the thread.3. Create the thread using pthread_createMake sure creation is successful by checking the return value.4. Pass any arguments need through’ arg (packing and unpacking arg list necessary.)5. #include <pthread.h> at the top of your header.6. Compile: g++ -o executable file.cc -lpthreadThread’s local data01/14/1912Variables declared within a thread (function) are called local data.Local (automatic) data associated with a thread are allocated on the stack. So these may be deallocated when a thread returns. So don’t plan on using locally declared variables for returning arguments. Plan to pass the arguments thru argument list passed from the caller or initiator of the thread.Thread termination (destruction)01/14/1913Implicit : Simply returning from the function executed by the thread terminates the thread. In this case thread’s completion status is set to the return value.Explicit : Use thread_exit. Prototype: void thread_exit(void *status);The single pointer value in status is available to the threads waiting for this thread.Waiting for thread exit01/14/1914int pthread_join (pthread_t tid, void * *statusp);A call to this function makes a thread wait for another thread whose thread id is specified by tid in the above prototype.When the thread specified by tid exits its completion status is stored and returned in statusp.The Thread Model01/14/1915(a) Three processes each with one thread(b) One process with three threadsImplementing Threads in User Space01/14/1916A user-level threads packageImplementing Threads in the Kernel01/14/1917A threads package managed by the kernelHybrid Implementations01/14/1918 Multiplexing user-level threads onto kernel- level threadsScheduler Activations01/14/1919Goal – mimic functionality of kernel threadsgain performance of user space threadsAvoids unnecessary user/kernel transitionsKernel assigns virtual processors to each processlets runtime system allocate threads to processorsProblem: Fundamental reliance on kernel (lower layer) calling procedures in user space (higher layer)Pop-Up Threads01/14/1920Creation of a new thread when message arrives(a) before message arrives(b) after message arrivesThread poolsThread Scheduling (1)01/14/1921Possible scheduling of user-level threads50-msec process quantumthreads run 5 msec/CPU burstB1, B2, B3Thread Scheduling (2)01/14/1922Possible scheduling of kernel-level threads50-msec process quantumthreads run 5 msec/CPU burstB1, B2, B3Summary01/14/1923We looked at Implementation of threads.thread-based concurrency.Pthread programmingWe will look at a pthread programming demoSee
View Full Document