SlideShare a Scribd company logo
Operating Systems Multi-threading models Tutorial on pthreads
Multithreading Models Many-to-One One-to-One Many-to-Many
Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads
Many-to-One Model
One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later
One-to-one Model
Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the  operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the  ThreadFiber  package
Many-to-Many Model
Thread Libraries Thread library  provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space: we will study in the next class how this can be implemented Kernel-level library supported by the OS
Thread Pools Create a number of threads in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool
Pthreads May be provided either as user-level or kernel-level A 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 Common in UNIX operating systems (Solaris, Linux, Mac OS X)
Pthreads: POSIX Threads Pthreads is a standard set of C library functions for multithreaded programming IEEE Portable Operating System Interface, POSIX, section 1003.1 standard, 1995 Pthread Library (60+ functions) Thread management: create, exit, detach, join, . . . Thread cancellation Mutex locks: init, destroy, lock, unlock, . . . Condition variables: init, destroy, wait, timed wait, . . . . . . Programs must include the file pthread.h Programs must be linked with the pthread library (-lpthread)
Pthreads Naming Convention Types: pthread[_object]_t Functions: pthread[_object]_action Constants/Macros: PTHREAD_PURPOSE Examples: pthread_t: the type of a thread pthread_create(): creates a thread pthread_mutex_t: the type of a mutex lock pthread_mutex_lock(): lock a mutex PTHREAD_CREATE_DETACHED
pthread_self() Returns the thread identifier for the calling thread At any point in its instruction stream a thread can figure out which thread it is Convenient to be able to write code that says: “If you’re thread 1 do this, otherwise do that” However, the thread identifier is an opaque object (just a pthread_t value) you must use pthread_equal() to test equality pthread_t   pthread_self (void); int   pthread_equal (pthread_t id1, pthread_t id2);
pthread_create() Creates a new thread int  pthread_create  ( pthread_t *thread, pthread_attr_t *attr, void * (*start_routine) (void *) , void *arg); Returns 0 to indicate success, otherwise returns error code thread : output argument for the id of the new thread attr : input argument that specifies the attributes of the thread to be created (NULL = default attributes) start_routine:  function to use as the start of the new thread must have prototype: void * foo(void*) arg:  argument to pass to the new thread routine If the thread routine requires multiple arguments, they must be passed bundled up in an array or a structure
pthread_create() example Want to create a thread to compute the sum of the elements of an array void *do_work(void *arg); Needs three arguments the array, its size, where to store the sum we need to bundle them in a structure struct arguments { double *array; int size; double *sum; }
pthread_create() example int main(int argc, char *argv) { double array[100]; double sum; pthread_t worker_thread; struct arguments *arg; arg = (struct arguments *)calloc(1, sizeof(struct arguments)); arg->array = array;  arg->size=100;  arg->sum = ∑ if ( pthread_create (&worker_thread, NULL,  do_work, (void *)arg)) { fprintf(stderr,”Error while creating thread\n”); exit(1); } ... }
pthread_create() example void *do_work(void *arg) { struct arguments *argument; int i, size; double *array; double *sum; argument = (struct arguments*)arg; size = argument->size; array = argument->array; sum = argument->sum; *sum = 0; for (i=0;i<size;i++) *sum += array[i];  return NULL; }
Comments about the example The “main thread” continues its normal execution after creating the “child thread” IMPORTANT : If the main thread terminates, then all threads are killed! We will see that there is a join() function Of course, memory is shared by the parent and the child (the array, the location of the sum) nothing prevents the parent from doing something to it while the child is still executing  which may lead to a wrong computation we will see that Pthreads provides mutual exclusion mechanisms The bundling and unbundling of arguments is a bit tedious
Memory Management of Args The parent thread allocates memory for the arguments Warning #1:  you don’t want to free that memory before the child thread has a chance to read it That would be a race condition Better to let the child do the freeing Warning #2:  if you create multiple threads you want to be careful there is no sharing of arguments, or that the sharing is safe For instance, if you reuse the same data structure for all threads and modify its fields before each call to pthread_create(), some threads may not be able to read the arguments destined to them Safest way: have a separate arg structure for each thread
pthread_exit() Terminates the calling thread void  pthread_exit (void *retval); The return value is made available to another thread calling a  pthread_join() The previous example had the thread just return from  function do_work() In this case the call to  pthread_exit()  is implicit The return value of the function serves as the argument to the (implicitly called)  pthread_exit() .
Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation  terminates the target thread  immediately Deferred cancellation  allows the target thread to periodically check if it should be cancelled
pthread_kill() Causes the termination of a thread int  pthread_kill ( pthread_t thread, int sig); thread : input parameter, id of the thread to terminate sig:  signal number returns 0 to indicate success, error code otherwise
pthread_join() Causes the calling thread to wait for another thread to terminate int  pthread_join ( pthread_t thread, void **value_ptr); thread : input parameter, id of the thread to wait on value_ptr : output parameter, value given to  pthread_exit()  by the terminating thread (which happens to always be a  void * ) returns 0 to indicate success, error code otherwise multiple simultaneous calls for the same thread are not allowed
pthread_join() example int main(int argc, char *argv) { double array[100]; double sum; pthread_t worker_thread; struct arguments *arg; void  *return_value; arg = (struct arguments *)calloc(1,sizeof(struct arguments)); arg->array = array;  arg->size=100;  arg->sum = &sum; if ( pthread_create (&worker_thread, NULL, do_work, (void *)arg)) { fprintf(stderr,”Error while creating thread\n”); exit(1); } ... if ( pthread_join (worker_thread, &return_value)) { fprintf(stderr,”Error while waiting for thread\n”); exit(1); } }
pthread_join()  Warning This is a common “bug” that first-time pthread programmers encounter Without the call to pthread_join() the previous program may end immediately, with the main thread reaching the end of main() and exiting, thus killing all other threads perhaps even before they have had a chance to execute
pthread_join()  Warning When creating multiple threads be careful to store the handle of each thread in a separate variable Typically one has an array of thread handles That way you’ll be able to call pthread_join() for each thread Also, note that the following code is sequential! for (i=0; i < num_threads; i++) { pthread_create(&(threads[i]),...) pthread_join(threads[i],...) }
Thread Attributes One of the parameters to pthread_create() is a  thread attribute In all our previous examples we have set it to NULL But it can be very useful and provides a simple way to set options: Initialize an attribute Set its value with some Pthread API call Pass it to Pthread API functions like pthread_create()
pthread_attr_init() Initialized the thread attribute object to the default values int  pthread_attr_init ( pthread_attr_t *attr); Return 0 to indicate success, error code otherwise attr : pointer to a thread attribute
Detached Thread One option when creating a thread is whether it is joinable or detached Joinable : another thread can call join on it By default a thread is joinable Detached : no thread can call join on it Let’s look at the function that allows to set the “detached state”
pthread_attr_setdetachstate() Sets the detach state attribute int  pthread_attr_setdetachstate ( pthread_attr_t *attr, int detachstate); returns 0 to indicate success, error code otherwise attr: input parameter, thread attribute detachstate: can be either PTHREAD_CREATE_DETACHED PTHREAD_CREATE_JOINABLE  (default)
Detach State Detached threads have all resources freed when they terminate Joinable threads have state information about the thread kept even after they finish To allow for a thread to join a finished thread  So-called “no rush to join” So, if you know that you will not need to join a thread, create it in a detached state so that you save resources This is lean-and-mean C, as opposed to hand-holding Java, and every little saving is important
Creating a Detached Thread #include <pthread.h> #define NUM_THREAD 25 void * thread_routine  (void *arg) { printf(“Thread %d, my TID is %u\n”, (int)arg, pthread_self()); pthread_exit(0); }
Creating a Detached Thread int main() { pthread_attr_t attr; pthread_t tids[NUM_THREADS]; int x; pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED ); phthread_create (&(tids[x]), &attr,  thread_routine,  (void *)x); . . .  // should take a while otherwise   . . .  // the child may not have time to run }

More Related Content

PDF
Pthread
PPTX
Input-Buffering
PPTX
Computer architecture multi processor
PDF
Monitors
PDF
Signal Handling in Linux
PDF
System calls
PPT
Inter process communication
PPTX
Presentation on Segmentation
Pthread
Input-Buffering
Computer architecture multi processor
Monitors
Signal Handling in Linux
System calls
Inter process communication
Presentation on Segmentation

What's hot (20)

PPT
Introduction to System Calls
PPT
Unix File System
PPS
Virtual memory
PPTX
Linux process management
PPTX
Computer architecture page replacement algorithms
PPTX
Syntax Analysis in Compiler Design
PPTX
Daa unit 1
PPT
Intermediate code generation (Compiler Design)
PPTX
Types of grammer - TOC
PPT
process creation OS
PDF
Distributed Operating System_1
PPTX
Multithreading models.ppt
PPTX
PPTX
PPTX
PDF
Introduction to OpenMP
PPTX
distributed memory architecture/ Non Shared MIMD Architecture
PPTX
java interface and packages
PDF
CS9222 ADVANCED OPERATING SYSTEMS
PDF
Parse Tree
Introduction to System Calls
Unix File System
Virtual memory
Linux process management
Computer architecture page replacement algorithms
Syntax Analysis in Compiler Design
Daa unit 1
Intermediate code generation (Compiler Design)
Types of grammer - TOC
process creation OS
Distributed Operating System_1
Multithreading models.ppt
Introduction to OpenMP
distributed memory architecture/ Non Shared MIMD Architecture
java interface and packages
CS9222 ADVANCED OPERATING SYSTEMS
Parse Tree
Ad

Viewers also liked (20)

PPTX
Threads
PPT
Operating System-Threads-Galvin
PPT
Multi threading
PPTX
Composite culture class 9 icse
PPTX
PACE-IT: Operation System Features
PPTX
Threads
PPTX
Creation of threads
PPTX
P-Threads
PDF
Basic Multithreading using Posix Threads
PDF
Thread dumps
PPTX
Basic Thread Knowledge
PPT
Learning Java 3 – Threads and Synchronization
PPT
Posix threads(asha)
PPT
Operating Systems
PPTX
Thread scheduling in Operating Systems
PPT
Posix Threads
PDF
Java Thread Synchronization
PPT
Ch5: Threads (Operating System)
PPTX
History of Operating system
PPT
Java Basics
Threads
Operating System-Threads-Galvin
Multi threading
Composite culture class 9 icse
PACE-IT: Operation System Features
Threads
Creation of threads
P-Threads
Basic Multithreading using Posix Threads
Thread dumps
Basic Thread Knowledge
Learning Java 3 – Threads and Synchronization
Posix threads(asha)
Operating Systems
Thread scheduling in Operating Systems
Posix Threads
Java Thread Synchronization
Ch5: Threads (Operating System)
History of Operating system
Java Basics
Ad

Similar to P threads (20)

PPT
Tutorial4 Threads
PDF
System Programming - Threading
PPTX
Understanding Threads in operating system
PDF
40d5984d819aaa72e55aa10376b73bde_MIT6_087IAP10_lec12.pdf
PDF
Linux Internals - Part III
PDF
posix.pdf
PPTX
ch 7 POSIX.pptx
PPTX
OPERATNG SYSTEM MODULE-2 PRESENTATION OS
PDF
Threads operating system slides easy understand
PDF
Pthread Library
PDF
Multithreaded Programming Part- II.pdf
PPTX
Essentials of Multithreaded System Programming in C++
PPTX
Engineeering Operating systemsOS UNIT 3 Threads.pptx
ODP
Sysprog 14
PPT
Lecture 9 -_pthreads-linux_threads
PDF
Consider the fork_examplec code under Example code for pr.pdf
PPT
Threaded Programming
PPT
Shared Memory Programming with Pthreads (1).ppt
Tutorial4 Threads
System Programming - Threading
Understanding Threads in operating system
40d5984d819aaa72e55aa10376b73bde_MIT6_087IAP10_lec12.pdf
Linux Internals - Part III
posix.pdf
ch 7 POSIX.pptx
OPERATNG SYSTEM MODULE-2 PRESENTATION OS
Threads operating system slides easy understand
Pthread Library
Multithreaded Programming Part- II.pdf
Essentials of Multithreaded System Programming in C++
Engineeering Operating systemsOS UNIT 3 Threads.pptx
Sysprog 14
Lecture 9 -_pthreads-linux_threads
Consider the fork_examplec code under Example code for pr.pdf
Threaded Programming
Shared Memory Programming with Pthreads (1).ppt

More from Dr. C.V. Suresh Babu (20)

PPTX
Data analytics with R
PPTX
Association rules
PPTX
PPTX
Classification
PPTX
Blue property assumptions.
PPTX
Introduction to regression
PPTX
Expert systems
PPTX
Dempster shafer theory
PPTX
Bayes network
PPTX
Bayes' theorem
PPTX
Knowledge based agents
PPTX
Rule based system
PPTX
Formal Logic in AI
PPTX
Production based system
PPTX
Game playing in AI
PPTX
Diagnosis test of diabetics and hypertension by AI
PPTX
A study on “impact of artificial intelligence in covid19 diagnosis”
PDF
A study on “impact of artificial intelligence in covid19 diagnosis”
Data analytics with R
Association rules
Classification
Blue property assumptions.
Introduction to regression
Expert systems
Dempster shafer theory
Bayes network
Bayes' theorem
Knowledge based agents
Rule based system
Formal Logic in AI
Production based system
Game playing in AI
Diagnosis test of diabetics and hypertension by AI
A study on “impact of artificial intelligence in covid19 diagnosis”
A study on “impact of artificial intelligence in covid19 diagnosis”

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Cloud computing and distributed systems.
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
Agricultural_Statistics_at_a_Glance_2022_0.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Chapter 3 Spatial Domain Image Processing.pdf
Cloud computing and distributed systems.
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
Approach and Philosophy of On baking technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Review of recent advances in non-invasive hemoglobin estimation
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx

P threads

  • 1. Operating Systems Multi-threading models Tutorial on pthreads
  • 2. Multithreading Models Many-to-One One-to-One Many-to-Many
  • 3. Many-to-One Many user-level threads mapped to single kernel thread Examples: Solaris Green Threads GNU Portable Threads
  • 5. One-to-One Each user-level thread maps to kernel thread Examples Windows NT/XP/2000 Linux Solaris 9 and later
  • 7. Many-to-Many Model Allows many user level threads to be mapped to many kernel threads Allows the operating system to create a sufficient number of kernel threads Solaris prior to version 9 Windows NT/2000 with the ThreadFiber package
  • 9. Thread Libraries Thread library provides programmer with API for creating and managing threads Two primary ways of implementing Library entirely in user space: we will study in the next class how this can be implemented Kernel-level library supported by the OS
  • 10. Thread Pools Create a number of threads in a pool where they await work Advantages: Usually slightly faster to service a request with an existing thread than create a new thread Allows the number of threads in the application(s) to be bound to the size of the pool
  • 11. Pthreads May be provided either as user-level or kernel-level A 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 Common in UNIX operating systems (Solaris, Linux, Mac OS X)
  • 12. Pthreads: POSIX Threads Pthreads is a standard set of C library functions for multithreaded programming IEEE Portable Operating System Interface, POSIX, section 1003.1 standard, 1995 Pthread Library (60+ functions) Thread management: create, exit, detach, join, . . . Thread cancellation Mutex locks: init, destroy, lock, unlock, . . . Condition variables: init, destroy, wait, timed wait, . . . . . . Programs must include the file pthread.h Programs must be linked with the pthread library (-lpthread)
  • 13. Pthreads Naming Convention Types: pthread[_object]_t Functions: pthread[_object]_action Constants/Macros: PTHREAD_PURPOSE Examples: pthread_t: the type of a thread pthread_create(): creates a thread pthread_mutex_t: the type of a mutex lock pthread_mutex_lock(): lock a mutex PTHREAD_CREATE_DETACHED
  • 14. pthread_self() Returns the thread identifier for the calling thread At any point in its instruction stream a thread can figure out which thread it is Convenient to be able to write code that says: “If you’re thread 1 do this, otherwise do that” However, the thread identifier is an opaque object (just a pthread_t value) you must use pthread_equal() to test equality pthread_t pthread_self (void); int pthread_equal (pthread_t id1, pthread_t id2);
  • 15. pthread_create() Creates a new thread int pthread_create ( pthread_t *thread, pthread_attr_t *attr, void * (*start_routine) (void *) , void *arg); Returns 0 to indicate success, otherwise returns error code thread : output argument for the id of the new thread attr : input argument that specifies the attributes of the thread to be created (NULL = default attributes) start_routine: function to use as the start of the new thread must have prototype: void * foo(void*) arg: argument to pass to the new thread routine If the thread routine requires multiple arguments, they must be passed bundled up in an array or a structure
  • 16. pthread_create() example Want to create a thread to compute the sum of the elements of an array void *do_work(void *arg); Needs three arguments the array, its size, where to store the sum we need to bundle them in a structure struct arguments { double *array; int size; double *sum; }
  • 17. pthread_create() example int main(int argc, char *argv) { double array[100]; double sum; pthread_t worker_thread; struct arguments *arg; arg = (struct arguments *)calloc(1, sizeof(struct arguments)); arg->array = array; arg->size=100; arg->sum = &sum; if ( pthread_create (&worker_thread, NULL, do_work, (void *)arg)) { fprintf(stderr,”Error while creating thread\n”); exit(1); } ... }
  • 18. pthread_create() example void *do_work(void *arg) { struct arguments *argument; int i, size; double *array; double *sum; argument = (struct arguments*)arg; size = argument->size; array = argument->array; sum = argument->sum; *sum = 0; for (i=0;i<size;i++) *sum += array[i]; return NULL; }
  • 19. Comments about the example The “main thread” continues its normal execution after creating the “child thread” IMPORTANT : If the main thread terminates, then all threads are killed! We will see that there is a join() function Of course, memory is shared by the parent and the child (the array, the location of the sum) nothing prevents the parent from doing something to it while the child is still executing which may lead to a wrong computation we will see that Pthreads provides mutual exclusion mechanisms The bundling and unbundling of arguments is a bit tedious
  • 20. Memory Management of Args The parent thread allocates memory for the arguments Warning #1: you don’t want to free that memory before the child thread has a chance to read it That would be a race condition Better to let the child do the freeing Warning #2: if you create multiple threads you want to be careful there is no sharing of arguments, or that the sharing is safe For instance, if you reuse the same data structure for all threads and modify its fields before each call to pthread_create(), some threads may not be able to read the arguments destined to them Safest way: have a separate arg structure for each thread
  • 21. pthread_exit() Terminates the calling thread void pthread_exit (void *retval); The return value is made available to another thread calling a pthread_join() The previous example had the thread just return from function do_work() In this case the call to pthread_exit() is implicit The return value of the function serves as the argument to the (implicitly called) pthread_exit() .
  • 22. Thread Cancellation Terminating a thread before it has finished Two general approaches: Asynchronous cancellation terminates the target thread immediately Deferred cancellation allows the target thread to periodically check if it should be cancelled
  • 23. pthread_kill() Causes the termination of a thread int pthread_kill ( pthread_t thread, int sig); thread : input parameter, id of the thread to terminate sig: signal number returns 0 to indicate success, error code otherwise
  • 24. pthread_join() Causes the calling thread to wait for another thread to terminate int pthread_join ( pthread_t thread, void **value_ptr); thread : input parameter, id of the thread to wait on value_ptr : output parameter, value given to pthread_exit() by the terminating thread (which happens to always be a void * ) returns 0 to indicate success, error code otherwise multiple simultaneous calls for the same thread are not allowed
  • 25. pthread_join() example int main(int argc, char *argv) { double array[100]; double sum; pthread_t worker_thread; struct arguments *arg; void *return_value; arg = (struct arguments *)calloc(1,sizeof(struct arguments)); arg->array = array; arg->size=100; arg->sum = &sum; if ( pthread_create (&worker_thread, NULL, do_work, (void *)arg)) { fprintf(stderr,”Error while creating thread\n”); exit(1); } ... if ( pthread_join (worker_thread, &return_value)) { fprintf(stderr,”Error while waiting for thread\n”); exit(1); } }
  • 26. pthread_join() Warning This is a common “bug” that first-time pthread programmers encounter Without the call to pthread_join() the previous program may end immediately, with the main thread reaching the end of main() and exiting, thus killing all other threads perhaps even before they have had a chance to execute
  • 27. pthread_join() Warning When creating multiple threads be careful to store the handle of each thread in a separate variable Typically one has an array of thread handles That way you’ll be able to call pthread_join() for each thread Also, note that the following code is sequential! for (i=0; i < num_threads; i++) { pthread_create(&(threads[i]),...) pthread_join(threads[i],...) }
  • 28. Thread Attributes One of the parameters to pthread_create() is a thread attribute In all our previous examples we have set it to NULL But it can be very useful and provides a simple way to set options: Initialize an attribute Set its value with some Pthread API call Pass it to Pthread API functions like pthread_create()
  • 29. pthread_attr_init() Initialized the thread attribute object to the default values int pthread_attr_init ( pthread_attr_t *attr); Return 0 to indicate success, error code otherwise attr : pointer to a thread attribute
  • 30. Detached Thread One option when creating a thread is whether it is joinable or detached Joinable : another thread can call join on it By default a thread is joinable Detached : no thread can call join on it Let’s look at the function that allows to set the “detached state”
  • 31. pthread_attr_setdetachstate() Sets the detach state attribute int pthread_attr_setdetachstate ( pthread_attr_t *attr, int detachstate); returns 0 to indicate success, error code otherwise attr: input parameter, thread attribute detachstate: can be either PTHREAD_CREATE_DETACHED PTHREAD_CREATE_JOINABLE (default)
  • 32. Detach State Detached threads have all resources freed when they terminate Joinable threads have state information about the thread kept even after they finish To allow for a thread to join a finished thread So-called “no rush to join” So, if you know that you will not need to join a thread, create it in a detached state so that you save resources This is lean-and-mean C, as opposed to hand-holding Java, and every little saving is important
  • 33. Creating a Detached Thread #include <pthread.h> #define NUM_THREAD 25 void * thread_routine (void *arg) { printf(“Thread %d, my TID is %u\n”, (int)arg, pthread_self()); pthread_exit(0); }
  • 34. Creating a Detached Thread int main() { pthread_attr_t attr; pthread_t tids[NUM_THREADS]; int x; pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED ); phthread_create (&(tids[x]), &attr, thread_routine, (void *)x); . . . // should take a while otherwise . . . // the child may not have time to run }