Presentation is loading. Please wait.

Presentation is loading. Please wait.

B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy.

Similar presentations


Presentation on theme: "B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy."— Presentation transcript:

1 B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy

2 2 Topics to be Covered Objective POSIX threads What are Threads? Creating threads Using threads Summary

3 B.Ramamurthy3 Objective To study POSIX standard for threads called Pthreads. To study thread control primitives for creation, termination, join, synchronization, concurrency, and scheduling. To learn to design multi-threaded applications.

4 B.Ramamurthy4 Creating threads Always include pthread library: #include int pthread_create (pthread_t *threadp, const pthread_attr_t * attr, void *(* start routine)(void *), void *arg); This creates a new thread of control that calls the function start_routine. It return a zero if the creation is successful, and thread id in threadp (first parameter). attr is to modify the attributes of the new thread. If it is NULL, default attributes are used. The arg is passing arguments to the thread function.

5 B.Ramamurthy5 Using threads 1. Declare a variable of type pthread_t 2. Define a function to be executed by the thread. 3. Create the thread using pthread_create Make sure creation is successful by checking the return value. 4. Pass any arguments need through’ arg (packing and unpacking arg list necessary.) 6. Compile: cc –o xyz xyz.c {options such as – lthread –lpthread}

6 B.Ramamurthy6 Thread’s local data Variables declared within a thread (function) are called local data. Local (static) data associated with a thread are allocated on the stack. So these may be deallocated when a thread returns. So don’t plan on using locally declared variables for returning arguments. Plan to pass the arguments thru argument list passed from the caller or initiator of the thread.

7 B.Ramamurthy7 Thread termination (destruction) Implicit : Simply returning from the function executed by the thread terminates the thread. In this case thread’s completion status is set to the return value. Explicit : Use thread_exit. Prototype: void thread_exit(void *status); The single pointer value in status is available to the threads waiting for this thread.

8 B.Ramamurthy8 Waiting for thread exit int pthread_join (pthread_t tid, void **statusp); A call to this function makes a thread wait for another thread whose thread_id is specified by tid in the above prototype. When the thread specified by tid exits its completion status is stored(returned) in statusp.

9 B.Ramamurthy9 Example See the multi_thr.c example from Pthreads book by Lewis and Berg. This is available in /projects/bina/Pthreads We will walk through the execution of this program to understand basic thread control. Anything specified in Uppercase are functions/macro made by the authors. Example: PTHREAD_CREATE

10 B.Ramamurthy10 PTHREAD_CREATE Is a robust version of the simple pthread_create of the posix library. {int err; if (err = pthread_create(new_thread_ID, attr, start_func, arg) { printf(%s\n”, strerror(err)); abort(); }

11 B.Ramamurthy11 Other pthread functions pthread_self : to get the “handle” for itself. Synchronization: pthread_mutex_t pthread_mutex_lock (&lock) pthread_mutex_unlock(&lock) sema_t, sema_init, sema_wait, sema_post, sema_destroy

12 B.Ramamurthy12 Summary We will look at the basic thread operation in this lecture. We will look into the details of critical section, need for synchronization, synchronization models in the next lecture.


Download ppt "B.Ramamurthy1 POSIX Thread Programming 2/14/97 B.Ramamurthy."

Similar presentations


Ads by Google