Table of Contents
Te collective memory multiprocessor architectures, threads can be used to implement parallelism. Historically, hardware vendors have implemented their own proprietary versions of threads, making portability a concern for software developers. For UNIX systems, a standardized C language threads programming interface has bot specified by the IEEE POSIX 1003.1c standard. Implementations that adhere to this standard are referred to spil POSIX threads, or Pthreads.
The tutorial embarks with an introduction to concepts, motivations, and vormgeving considerations for using Pthreads. Each of the three major classes of routines te the Pthreads API are then covered: Thread Management, Mutex Variables, and Condition Variables. Example codes are used via to demonstrate how to use most of the Pthreads routines needed by a fresh Pthreads programmer. The tutorial concludes with a discussion of LLNL specifics and how to mix MPI with pthreads. A laboratorium exercise, with numerous example codes (C Language) is also included.
Level/Prerequisites: This tutorial is ideal for those who are fresh to parallel programming with pthreads. A basic understanding of parallel programming ter C is required. For those who are unacquainted with Parallel Programming ter general, the material covered ter EC3500: Introduction to Parallel Computing would be helpful.
What is a Thread?
To the software developer, the concept of a “proces” that runs independently from its main program may best describe a thread.
To go one step further, imagine a main program (a.out) that contains a number of procedures. Then imagine all of thesis procedures being able to be scheduled to run at the same time and/or independently by the operating system. That would describe a “multi-threaded” program.
How is this accomplished?
- Technically, a thread is defined spil an independent stream of instructions that can be scheduled to run spil such by the operating system. But what does this mean?
- Before understanding a thread, one very first needs to understand a UNIX process. A process is created by the operating system, and requires a fair amount of “overhead”. Processes contain information about program resources and program execution state, including:
Threads use and exist within thesis process resources, yet are able to be scheduled by the operating system and run spil independent entities largely because they duplicate only the nude essential resources that enable them to exist spil executable code.
This independent flow of control is accomplished because a thread maintains its own:
Scheduling properties (such spil policy or priority)
Set of pending and blocked signals
Thread specific gegevens.
So, te summary, ter the UNIX environment a thread:
Exists within a process and uses the process resources
Has its own independent flow of control spil long spil its parent process exists and the OS supports it
Duplicates only the essential resources it needs to be independently schedulable
May share the process resources with other threads that act identically independently (and dependently)
Dies if the parent process dies – or something similar
Is “lightweight” because most of the overhead has already bot accomplished through the creation of its process.
Because threads within the same process share resources:
Switches made by one thread to collective system resources (such spil closing a verkeersopstopping) will be seen by all other threads.
Two pointers having the same value point to the same gegevens.
Reading and writing to the same memory locations is possible, and therefore requires explicit synchronization by the programmer.
- Process ID, process group ID, user ID, and group ID
- Working directory.
- Program instructions
- Verkeersopstopping descriptors
- Signal deeds
- Collective libraries
- Inter-process communication devices (such spil message queues, pipes, semaphores, or collective memory).
What are Pthreads?
Te order to take total advantage of the capabilities provided by threads, a standardized programming interface wasgoed required.
For UNIX systems, this interface has bot specified by the IEEE POSIX 1003.1c standard (1995).
Implementations adhering to this standard are referred to spil POSIX threads, or Pthreads.
Most hardware vendors now offerande Pthreads te addition to their proprietary API’s.
The POSIX standard has continued to evolve and fall under revisions, including the Pthreads specification.
Pthreads are defined spil a set of C language programming types and proces calls, implemented with a pthread.h header/include opstopping and a thread library – however this library may be part of another library, such spil libc, te some implementations.
- Historically, hardware vendors have implemented their own proprietary versions of threads. Thesis implementations differed substantially from each other making it difficult for programmers to develop portable threaded applications.
When compared to the cost of creating and managing a process, a thread can be created with much less operating system overhead. Managing threads requires fewer system resources than managing processes.
For example, the following table compares timing results for the fork() subroutine and the pthread_create() subroutine. Timings reflect 50,000 process/thread creations, were performed with the time utility, and units are ter seconds, no optimization flags.
Note: don’t expect the sytem and user times to add up to real time, because thesis are SMP systems with numerous CPUs/cores working on the problem at the same time. At best, thesis are approximations run on local machines, past and present.
Efficient Communications/Gegevens Exchange:
The primary motivation for considering the use of Pthreads te a high spectacle computing environment is to achieve optimum voorstelling. Te particular, if an application is using MPI for on-node communications, there is a potential that spectacle could be improved by using Pthreads instead.
MPI libraries usually implement on-node task communication via collective memory, which involves at least one memory copy operation (process to process).
For Pthreads there is no intermediate memory copy required because threads share the same address space within a single process. There is no gegevens transfer, vanaf se. It can be spil efficient spil simply passing a pointer.
Te the worst case screenplay, Pthread communications become more of a cache-to-CPU or memory-to-CPU bandwidth punt. Thesis speeds are much higher than MPI collective memory communications.
For example: some local comparisons, past and present, are shown below:
Other Common Reasons:
Threaded applications opoffering potential vertoning gains and practical advantages overheen non-threaded applications ter several other ways:
Overlapping CPU work with I/O: For example, a program may have sections where it is performing a long I/O operation. While one thread is waiting for an I/O system call to accomplish, CPU intensive work can be performed by other threads.
Priority/real-time scheduling: tasks which are more significant can be scheduled to supersede or interrupt lower priority tasks.
Asynchronous event treating: tasks which service events of indeterminate frequency and duration can be interleaved. For example, a web server can both transfer gegevens from previous requests and manage the arrival of fresh requests.
A flawless example is the typical web browser, where many interleaved tasks can be happening at the same time, and where tasks can vary ter priority.
Another good example is a modern operating system, which makes extensive use of threads. A screenshot of the MS Windows OS and applications using threads is shown below.
Click for larger picture
Designing Threaded Programs
There are many considerations for designing parallel programs, such spil:
What type of parallel programming prototype to use?
Synchronization and wedren conditions
Covering thesis topics is beyond the scope of this tutorial, however interested readers can obtain a quick overview te the Introduction to Parallel Computing tutorial.
Te general tho’, ter order for a program to take advantage of Pthreads, it voorwaarde be able to be organized into discrete, independent tasks which can execute concurrently. For example, if routine1 and routine2 can be interchanged, interleaved and/or overlapped ter real time, they are candidates for threading.
Programs having the following characteristics may be well suited for pthreads:
Work that can be executed, or gegevens that can be operated on, by numerous tasks at the same time:
Block for potentially long I/O waits
Use many CPU cycles te some places but not others
Vereiste react to asynchronous events
Some work is more significant than other work (priority interrupts)
Several common models for threaded programs exist:
Manager/worker: a single thread, the manager assigns work to other threads, the workers. Typically, the manager treats all input and parcels out work to the other tasks. At least two forms of the manager/worker specimen are common: static worker pool and dynamic worker pool.
Pipeline: a task is violated into a series of suboperations, each of which is treated ter series, but concurrently, by a different thread. An automobile assembly line best describes this specimen.
Peer: similar to the manager/worker prototype, but after the main thread creates other threads, it participates te the work.
Collective Memory Proefje:
Threads also have their own private gegevens
Programmers are responsible for synchronizing access (protecting) globally collective gegevens.
- All threads have access to the same global, collective memory
For example, suppose that your application creates several threads, each of which makes a call to the same library routine:
This library routine accesses/modifies a global structure or location ter memory.
Spil each thread calls this routine it is possible that they may attempt to modify this global structure/memory location at the same time.
If the routine does not employ some sort of synchronization constructs to prevent gegevens corruption, then it is not thread-safe.
- Thread-safeness: ter a nutshell, refers an application’s capability to execute numerous threads at the same time without “clobbering” collective gegevens or creating “wedren” conditions.
Recommendation: Be careful if your application uses libraries or other objects that don’t explicitly ensure thread-safeness. When te doubt, assume that they are not thread-safe until proven otherwise. This can be done by “serializing” the calls to the uncertain routine, etc.
- The implication to users of outer library routines is that if you aren’t 100% certain the routine is thread-safe, then you take your chances with problems that could arise.
Because of this, a program that runs fine on one toneelpodium, may fail or produce wrong results on another toneel.
For example, the maximum number of threads permitted, and the default thread stack size are two significant boundaries to consider when designing your program.
Several thread thresholds are discussed te more detail straks ter this tutorial.
- Albeit the Pthreads API is an ANSI/IEEE standard, implementations can, and usually do, vary ter ways not specified by the standard.
Copies of the standard can be purchased from IEEE or downloaded for free from other sites online.
The subroutines which comprise the Pthreads API can be informally grouped into four major groups:
Thread management: Routines that work directly on threads – creating, detaching, joining, etc. They also include functions to set/query thread attributes (joinable, scheduling etc.)
Mutexes: Routines that overeenkomst with synchronization, called a “mutex”, which is an abbreviation for “mutual exclusion”. Mutex functions provide for creating, ruining, locking and unlocking mutexes. Thesis are supplemented by mutex attribute functions that set or modify attributes associated with mutexes.
Condition variables: Routines that address communications inbetween threads that share a mutex. Based upon programmer specified conditions. This group includes functions to create, demolish, wait and signal based upon specified variable values. Functions to set/query condition variable attributes are also included.
Synchronization: Routines that manage read/write locks and barriers.
Naming conventions: All identifiers te the threads library start with pthread_. Some examples are shown below.
- The original Pthreads API wasgoed defined ter the ANSI/IEEE POSIX 1003.1 – 1995 standard. The POSIX standard has continued to evolve and fall under revisions, including the Pthreads specification.
Related movie: Cara Daftar di Bitcoin.co.id Serta Cara Menggunakannya