Thread Model for Concurrency

Review from last class: Can you give me a reference for unix process management? See Unix Process Management

Hmm...I don't know what do to for my term project?
See Term Project Directions

Thread Model

A thread refers to a thread of control flow: an independent sequence of execution of program code.

Threads are powerful. As with most powerful tools, if they are not used appropriately thread programming may be inefficient. Thread programming has become viable solution for many problems with the advent of multi-core processors Typically these problems are expected to handle many requests simultaneously. Example: multi-media, games, automotive embedded systems Especially relevant to embedded system with the proliferation of multi-core processors

Many thread models emerged: Solaris threads, win-32 threads 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. This library is a collection of C functions.


Lets understand how a thread library fits within out programming model adapted from F. Mueller's paper.

How do you create a thread?

Hmm... Where can I get more information to read on Posix thread? Here is a Tutorial.

Working with pThreads

Lets review pthreads using the 5 programs thread1.c, thread2.c...thread5.c.
  1. How do you create a pthread? What are the parameters when calling pthread? What values are returned?

    int rc = pthread_create( ptid, attr, funcWork, (void *)parameters);
    ptid - id of the new thread created; attr: attributes of the thread created; payload for the thread: function for the thread to execute; parameters for the function.
    The pthread_create returns a negative number there was problem creating the thread. The function returns the results computed through the parameter list. See thread1.c

  2. Aside: Explain static, automatic, and dynamic variable.
    Look at the page in GNU library explaining these types.

  3. How do you pass more than one parameter (of dfferent types) to a pthread?

    Create a struct with the parameters and pass the pointer to an instant of the struct. Inside the function access the elements of the struct instant using dynamic pointers. See thread2.c for example.
    In this example, we create an array of N threads each with its own parameter struct. So we see an array of structs.

  4. How can you synchronize or make the main thread wait for children thread to finish their work and join the main thread?
    (i) by setting one of the attributes of pthread to PTHREAD_CREATE_JOINABLE and (ii) by using pthread_join function to wait.
    When a pthread_join is executed it waits for a specified thread to exit. See thread3.c
  5. How do you protect a critical resource shared by many threads?
    Using pthread_mutex_t mutexsum; and functons pthread_mutex_lock(&mutexsum) and pthread_mutex_unlock(&mutexsum). See thread5.c

  6. This leads us into our next discussion on crtical resources, crtical section and mutually exclusive access to these resources.