POSIX Thread Programming - PowerPoint PPT Presentation

About This Presentation
Title:

POSIX Thread Programming

Description:

To study thread control primitives for creation, termination, join, ... See the multi_thr.c example from Pthreads book by Lewis and Berg. ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 13
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: POSIX Thread Programming


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

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

3
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
Creating threads
  • Always include pthread library
  • include ltpthread.hgt
  • 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
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
Threads 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 dont 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
Thread termination (destruction)
  • Implicit Simply returning from the function
    executed by the thread terminates the thread. In
    this case threads 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
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
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
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
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
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.
Write a Comment
User Comments (0)
About PowerShow.com