--- tags: Operating System --- # Multithreaded Programming ## What is thread * basic unit of CPU utilization * consist of * thread ID * program counter * register set * stack * Can share other threads within the same process * Single vs. multiple threaded programming: ![](https://i.imgur.com/jCpvkwy.png) ## Thread benefit * Responsiveness: Multithreading may allow a program to contiune running even if part of it is blocked or performing a lengthy operation * Resource sharing Can share memory and resources of the process, which can allow applications to have differeent threads of activity within the same address space. * Economy Empirically to ceate and manage processes is more time-consuming than threads * Scalability Threads can run in parallel on different processing cores. ## Types of thread (user-lever & kernel-level) * User threads supported above kernel and managed without kernel support * Kernel threads support directly by OS, including Windows, Linux, MacOS, Solaris ### Many-to-One model * Map many user-level threads to one kernel thread * Easy to manage but entire process will block when there is a blocking call in a thread * Unable to run in parallel on multicore systems ![](https://i.imgur.com/bo96hnO.png) ### One-to-one model * Map each user thread to a kernel thread * Provide more concurrency than many-to-one * Creating a user thread requires creating the corresponding kernel thread, which will burden the performance ![](https://i.imgur.com/IJJTcGP.png) ### Many-to-Many model * Multiplex user-level threads to smaller threads of kernel * can create as many user threads as necessary, and the corresponding kernel threads can run in parallel on a multiprocessor. And when a thread performs a blocking system call, the kernel can schedule another thread for execution ![](https://i.imgur.com/fyfnKFd.png) ## Thread Libraries There are two kinds of approaches to implement a thread library 1. User space: The entire library is in the user space without kernel support, which will lead the local function call in user space rather than system call 2. Kernel Level: The code and data structures for the library are located in the kernel space, which will result in the system call when invoking the function of thread Three common thread libraries nowadays: * POSIX Pthreads * Windows * Java ## Implicit threading Due to the growth of multiple processing, there are some challenges for programming: 1. Identifying tasks (劃分活動) 2. Balance (平衡) 3. Data splitting (資料分割) 4. Data dependency (資料依存性) 6. Testing and debugging (測試與除錯) To address the above difficulties, there is one way that can transfer the creation and management of threading from application developers to compliers and run-time libraries, which is called "implicit threading". The implicit threading can be divided into the following three parts. 1. Thread pool: To create a number of threads at process startup and place them into a pool, where they sit and wait for work 2. OpenMP: To provide support for the parallel programming in shared-memory environments 3. GCD: For iOS and MacOS developers, Grand Central Dispatch (GCD), is a combination of extension to the C language, an API, and a run-time library that allows application developers to identify sections of code to run in parallel like OpenMP