SlideServe
Menu
Préférences
© SlideServe · v2026

Pthread Programming

Mia_John avatar
19 diapositives · 764 Views · Apr 30, 2013

Pthread Programming. 출처 : http://www.engin.umd.umich.edu/~jinhua/winter03/cis450/calendar.htm CIS450 Winter 2003 Univ. of Michigan - Dearborn Professor Jinhua Guo. What are Pthreads? . Historically, hardware vendors have implemented their own proprietary versions of threads. Not portable.

Pthread Programming - slide 1
01 / 19Pthread Programming
1 / 17

Télécharger la présentation

Pthread Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Pthread Programming 출처: http://www.engin.umd.umich.edu/~jinhua/winter03/cis450/calendar.htm CIS450 Winter 2003 Univ. of Michigan - Dearborn Professor Jinhua Guo

  2. What are Pthreads? • Historically, hardware vendors have implemented their own proprietary versions of threads. Not portable. • Pthread is a standardized thread programming interface specified by the IEEE POSIX (portable operating systems interface) in 1995. • Pthreads are defined as a set of C language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library.

  3. Why Pthread ? • To realize potential program performance gains. • 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. • All threads within a process share the same address space. Inter-thread communication is more efficient and in many cases, easier to use than inter-process communication.

  4. Why Pthread ? • Threaded applications offer potential performance gains and practical advantages over non-threaded applications in several other ways: • Overlapping CPU work with I/O • Priority/real-time scheduling: tasks which are more important can be scheduled to supersede or interrupt lower priority tasks. • Asynchronous event handling: tasks which service events of indeterminate frequency and duration can be interleaved. For example, a web server can both transfer data from previous requests and manage the arrival of new requests.

  5. Include files and libraries • .h file #include <pthread.h> #include <semaphore.h> //for semaphore only • Compile and Linking $gcc  foo.c  -o foo  -lpthread –lrt (for semaphore)

  6. The Pthreads API • Thread management: The first class of functions work directly on threads - creating, terminating, joining, etc. • Semaphores: provide for create, destroy, wait, and post on semaphores. • Mutexes: provide for creating, destroying, locking and unlocking mutexes. • Condition variables: include functions to create, destroy, wait and signal based upon specified variable values.

  7. Thread Creation pthread_create (tid, attr, start_routine, arg) • It returns the new thread ID via the tid argument. • The attr parameter is used to set thread attributes, NULL for the default values. • The start_routine is the C routine that the thread will execute once it is created. • A single argument may be passed to start_routine via arg. It must be passed by reference as a pointer cast of type void.

  8. Thread Termination and Join pthread_exit (value) ; • This Function is used by a thread  to terminate.  The return value is passed as a pointer. pthread_join (tid, value_ptr); • The pthread_join() subroutine blocks the calling thread until the specified threadid thread terminates. • Return 0 on success, and negative on failure.  The returned value is a pointer returned by reference.  If you do not care about the return value, you can pass NULL for the second argument.

  9. Example Code - Pthread Creation and Termination #include <pthread.h> #include <stdio.h> void *PrintHello(void * id) { printf(“Thread%d: Hello World!\n", id); pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t thread0, thread1; pthread_create(&thread0, NULL, PrintHello, (void *) 0); pthread_create(&thread1, NULL, PrintHello, (void *) 1); pthread_exit(NULL); }

  10. Thread Identifiers pthread_self (); pthread_equal (tid1,tid2); • The pthread_self() routine returns the unique, system assigned thread ID of the calling thread. • The pthread_equal() routine compares two thread IDs. If the two IDs are different 0 is returned, otherwise a non-zero value is returned.

  11. Mutex Variables • For thread synchronization and protecting shared data when multiple writes occur. • A mutex variable acts like a "lock" protecting access to a shared data resource. • Only one thread can lock (or own) a mutex variable at any given time. Thus, even if several threads try to lock a mutex only one thread will be successful. No other thread can own that mutex until the owning thread unlocks that mutex. Threads must "take turns" accessing protected data.

  12. Creating / Destroying Mutexes pthread_mutex_init (mutex, attr) pthread_mutex_destroy (mutex) • Mutex variables must be declared with type pthread_mutex_t, and must be initialized before they can be used. • attr, mutex object attributes, specified as NULL to accept defaults

  13. Locking / Unlocking Mutexes pthread_mutex_lock (mutex); pthread_mutex_trylock (mutex); pthread_mutex_unlock (mutex); • The pthread_mutex_lock() routine is used by a thread to acquire a lock on the specified mutex variable. The thread blocks if the mutex is already locked by another thread. • pthread_mutex_trylock() will attempt to lock a mutex. However, if the mutex is already locked, the routine will return immediately with a "busy" error code. • pthread_mutex_unlock() will unlock a mutex if called by the owning thread.

  14. Semaphores • Semaphore are not defined in the POSIX.4a (pthread) specifications, but they are included in the POSIX.4 (realtime extension) specifications. • .h #include <semaphore.h> • Semaphore descriptors are declared global ex: sem_t mutex, full, empty;

  15. Routines of Semaphore sem_t sp; • sem_init(&sp, pshared, init_value) • If pshared is nonzero, the semaphore can be shared between processes. • sem_destroy(&sp) • sem_wait (&sp); //P operation • sem_trywait(&sp); • sem_post (&sp); // V operation

  16. Condition Variables • While mutexes implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data. • Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met. This can be very resource consuming since the thread would be continuously busy in this activity. A condition variable is a way to achieve the same goal without polling. • A condition variable is always used in conjunction with a mutex lock.

  17. Conditional Variable Routines • pthread_cond_init (condition, attr) • pthread_cond_destroy (condition) • pthread_cond_wait (condition, mutex) • pthread_cond_signal (condition) • pthread_cond_broadcast (condition)

  18. A representative sequence for using condition variables

  19. References • POSIX thread programming http://www.llnl.gov/computing/tutorials/workshops/workshop/index.html • "Multithreaded, Parallel, and Distributed Programming" by Gregory R. Andrews. • Introduction to PThreads http://phoenix.liunet.edu/%7Emdevi/pthread/Main.htm • Getting Started With POSIX Threads http://dis.cs.umass.edu/%7Ewagner/threads_html/tutorial.html