Download presentation
Presentation is loading. Please wait.
1
Chien-Chung Shen CIS/UD cshen@udel.edu
Chapter 27 Thread API Chien-Chung Shen CIS/UD
2
Introduction How to create and control threads?
What interfaces should the OS present for thread creation and control? How should these interfaces be designed to enable ease of use as well as utility? Use POSIX thread library as an example
3
Thread Creation C function pointer start_routing specifies which function should this thread start running in Why void pointers (void *)? having a void pointer as an argument to start_routine allows us to pass in any type of argument having it as a return value allows the thread to return any type of result
4
Thread Creation - example
5
Thread Completion Wait for a thread to complete
thread: which thread to wait for value_ptr: pointer to return value (a void pointer) expected to get back
6
361/OSTEP/t4.c return a single value pass in a single value
7
361/OSTEP/t3.c 361/OSTEP/t5.c pay attention to compiler warning
void *mythread(void *arg) { myarg_t *m = (myarg_t *) arg; printf("%d %d\n", m->a, m->b); myret_t r; r.x=1; r.y=2; return (void *) &r; } // ALLOCATED ON STACK: BAD! 361/OSTEP/t5.c pay attention to compiler warning
8
Thread ID P. 11.2 APUE/threads/threadid.c
The need to sleep in the main thread if it doesn’t sleep, the main thread might exit, thereby terminating the entire process before the new thread gets a chance to run this behavior is dependent on the operating system’s threads implementation and scheduling algorithms The new thread obtains its thread ID (unsigned long) by calling pthread_self() instead of reading it out of shared memory or receiving it as an argument to its thread-start routine the new thread may run before the main thread returns from calling pthread_create(), then the new thread will see the uninitialized contents of ntid
9
Lock Use lock to protect critical section Lock initialization
When done with the lock or
10
Condition Variable Useful when some kind of signaling must take place between threads, if one thread is waiting for another to do something before it can continue To use a condition variable, one has to, in addition, have a lock that is associated with this condition; when calling either of the above routines, this lock should be held pthread_cond_wait() puts the calling thread to sleep, and thus waits for some other thread to signal it, usually when something has changed that the now-sleeping thread might care about
11
Usage of Condition Variable
when signaling (as well as when modifying global variable ready), always make sure to have the lock held wait takes a lock as its second parameter, whereas signal only takes a condition wait, in addition to putting the calling thread to sleep, releases the lock when putting said caller to sleep however, before returning after being woken, re-acquires the lock, thus ensuring that any time the waiting thread is running between the lock acquire at the beginning of the wait sequence, and the lock release at the end, it holds the lock the waiting thread re-checks the condition in a while loop, instead of an if statement.
12
fork with pthread If we call fork(2) in a multi-threaded environment, the thread doing the call is now the main-thread in the new (child) process and all the other threads, which ran in the parent process, are dead. And everything they did was left exactly as it was just before the call to fork(2) Inside the child process, only one thread exists, which is made from a copy of the thread that called fork in the parent Stevens and Rago’s Section 12.9
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.