Download presentation
Presentation is loading. Please wait.
1
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Threads (part II) Copyright ©: Nahrstedt, Angrave, Abdelzaher
2
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Pthread Operations POSIX function description pthread_create create a thread pthread_detach set thread to release resources pthread_equal test two thread IDs for equality pthread_exit exit a thread without exiting process pthread_kill send a signal to a thread pthread_join wait for a thread pthread_self find out own thread ID
3
Exiting and Cancellation
Copyright ©: Nahrstedt, Angrave, Abdelzaher Exiting and Cancellation Question: If a thread calls exit(), what about other threads in the same process? When does a process terminate?
4
Exiting and Cancellation
Copyright ©: Nahrstedt, Angrave, Abdelzaher Exiting and Cancellation Question: If a thread calls exit(), what about other threads in the same process? A process terminates when: it calls exit directly one of its threads calls exit it returns from main() it receives a termination signal In any of these cases, all threads of the process terminate.
5
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Thread Exit void pthread_exit(void *value_ptr); Common uses: Return value is often a pointer to a malloc’d struct (memory must be free’d by joining thread) Can I return the address of a variable on the stack? 5 5
6
Thread Exit: pthread_exit or return?
Copyright ©: Nahrstedt, Angrave, Abdelzaher Thread Exit: pthread_exit or return? When a thread is done, it can return from its first function (the one used by pthread_create) or it can call pthread_exit An implicit call to pthread_exit() is made when a thread other than the thread in which main() was first invoked returns from the start routine that was used to create it. The function’s return value shall serve as the thread’s exit status. (from man pages) 6 6
7
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Cancel that thread! One thread can request that another exits with pthread_cancel int pthread_cancel(pthread_t thread); The pthread_cancel returns after making the request. The cancellation action in the target thread shall run asynchronously with respect to the calling thread returning from pthread_cancel(). Quiz: Do you see any danger by canceling a thread at an arbitrary point of its execution?
8
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Cancel that thread! Quiz: Do you see any danger by canceling a thread at an arbitrary point of its execution? Asynchronous cancellation immediately terminates another thread wherever it is in its execution sequence. This is dangerous and should be avoided (think about the case when a thread locks a shared resource and then it is cancelled while still inside its critical section) 8 8
9
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Cancel that thread! Cancellation is the mechanism by which a thread can terminate the execution of another thread. Depending on its settings, the target thread can then either ignore the request, honor it immediately, or defer it till it reaches a cancellation point. Threads are always created by pthread_create with cancellation enabled and deferred. That is, the initial cancellation state is PTHREAD_CANCEL_ENABLE and the initial type is PTHREAD_CANCEL_DEFERRED. 9 9
10
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Cancel that thread! When a thread eventually honors a cancellation request, it performs as if pthread_exit(PTHREAD_CANCELED) has been called at that point; hence, the thread stops executing with the return value PTHREAD_CANCELED. void pthread_testcancel(void) is a deferred cancellation point Other functions can also act as deferred cancellation points like blocking operations (such as waiting on a mutex lock or an I/O operation) 10 10
11
Zombies, Thread Detach & Join
Copyright ©: Nahrstedt, Angrave, Abdelzaher Zombies, Thread Detach & Join Call pthread_join() or pthread_detach() for every thread that is created joinable so that the system can reclaim all resources associated with the thread Failure to join or to detach threads memory and other resources will leak until the process ends
12
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Detaching a Thread int pthread_detach(pthread_t threadid); Indicate that system resources for the specified thread should be reclaimed when the thread ends If the thread has already ended, resources are reclaimed immediately This routine does not cause the thread to end Threads are detached after a pthread_detach() call after a pthread_join() call if a thread terminates and the PTHREAD_CREATE_DETACHED attribute was set on creation
13
How to make a Thread Detached
Copyright ©: Nahrstedt, Angrave, Abdelzaher How to make a Thread Detached void *processfd(void *arg); int error; int fd pthread_t tid; if (error = pthread_create(&tid, NULL, processfd, &fd)) { fprintf(stderr, "Failed to create thread: %s\n", strerror(error)); } else if (error = pthread_detach(tid)){ fprintf(stderr, "Failed to detach thread: %s\n", strerror(error));
14
How a thread can detach itself before terminating
Copyright ©: Nahrstedt, Angrave, Abdelzaher How a thread can detach itself before terminating void *runnger(void *arg) { … /* return NULL after detaching itself */ if (!pthread_detach( pthread_self()) ) return NULL; }
15
“Waiting” on a Thread: pthread_join()
Copyright ©: Nahrstedt, Angrave, Abdelzaher “Waiting” on a Thread: pthread_join() int pthread_join(pthread_t thread, void** retval); pthread_join() is a blocking call on non-detached threads It indicates that the caller wishes to block until the thread being joined exits You cannot join on a detached thread, only non-detached threads (detaching means you are NOT interested in knowing about the thread’s exit) It is a cancellation point
16
Copyright ©: Nahrstedt, Angrave, Abdelzaher
Pthread_join int error; void *exitcodep; pthread_t tid; … if (error = pthread_join(tid, &exitcodep)){ fprintf(stderr, "Failed to join thread: %s\n", strerror(error)); } else { fprintf(stderr, "The exit code was %d\n", (int)exitcodep);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.