• Mode-1 Multi-Core • Memory Allocators • OpenMP • Intel TBB • Pthreads • Java - Threads • Charm++ Prog. • Message Passing (MPI) • MPI - OpenMP • MPI - Intel TBB • MPI - Pthreads • Compiler Opt. Features • Threads-Perf. Math.Lib. • Threads-Prof. & Tools • Threads-I/O Perf. • PGAS : UPC / CAF / GA • Power-Perf. • Home




Programming on Multi-Core Processors Using Pthreads (POSIX Threads)

Pthreads are defined as a set of C-language programming types and procedure calls, implemented with a pthread.h header/include file and a thread library.

In many situations, data is read more often than it is modified or written. In these cases, you can allow threads to read concurrently while holding the lock and allow only one thread to hold the lock when data is modified. A multiple-reader single-writer lock (or read-write lock) does this. A read-write lock is acquired either for reading or writing, and then is released.
A read-write lock allows for a greater level of concurrency in accessing shared data than that permitted by a mutual exclusion lock. It exploits the fact that while only a single thread at a time (a writer thread) can modify the shared data, in many cases any number of threads can concurrently read the data (hence reader threads).

Programs that illustrate the use of Read-Write Lock using different read-write lock APIs are described.Sample demo code that gives basic idea of how to use Read-Write Lock and one sample application using both mutex and Read-Write Lock is described so that one can get better idea of what is exact difference between these synchronization constructs and how to use them.


Example 5.1
Write a sample Pthread program to illustrate the use of Read-Write Lock .
Example 5.2

Write a Pthread program to find minimum value in an Integer array using Mutex.
Example 5.3

Write a Pthread program to find minimum value in an Integer array using Read Write Lock.
(Source - References : Books     Multi-threading -[MCMTh-01], [MCMTh-02], [MCMTh-I03], [MCMTh-05], [MCMth-09], [MCMth-11], [MCMTh-15], [MCMTh-21], [MCBW-44] )

Description of Pthread Programs



Example 5.1 : Write a sample Pthread program to illustrate the use of Read-Write Lock .
( Download source code : pthread-readwritelock-demo.c )


  • Objective
  • Write a Pthread program to illustrate the use of Read-Write Lock.

  • Description
  • This is a very simple program to illustrate the use of Read-Write Lock APIs. The implementation is as follows: The main thread creates two child threads - rdlockThread and wrlockThread. The rdlockThread gets the read lock,after it gets it intentionally some wait time is introduced. During this time the wrlockThread gets the turn and acquires the write lock and unlocks it. Though there is no actual parallelism involved, this is just to demonstrate the working Read-Write lock APIs.

  • Input
  • None

  • Output
  • Sequence of acquiring and releasing Read-Write Lock.


Example 5.2 : Write a Pthread program to find the minimum value in an unsorted Integer array using mutex implementation  (compare this code with Ex.1.3).
( Download source code : pthread-findingmin-in-integerlist-mutex.c )


  • Objective
  • Write a Pthread program to find the minimum value in an unsorted Integer array using mutex implementation.

  • Description
  • This is pthread code for finding minimum value in an large integer array. Here array is divided into different chunks and each thread will find minimum value in its own chunk. The mutex locks are used to gain exclusive access to the finalminimum value variable. Once this access is gained, the value is updated as required, and the lock subsequently released. Since at any time, only one thread can hold the lock, only one thread can update the value

  • Input
  • Array Length, Number of threads

  • Output
  • The minimum value and Execution time.


Example 5.3 : Write a Pthread program to find the minimum value in an unsorted Integer using read-write lock implementation.
( Download source code : pthread-findingmin-in-integerlist-readwrite-locks.c )


  • Objective
  • Write a Pthread program to find the minimum value in an unsorted Integer array using read-write lock implementation.

  • Description
  • This is pthread code for finding minimum value in an large integer array based on read-write locks. Read-write locks are more suitable for application where frequency of read operation is more than write operations. So we can see performance improvement if we use read-write lock in such case. In this example read lock is allowing many threads to check(read) global minimum value and only if its local minimum value is less than global minimum value it will request write lock and then updates final value.

  • Input
  • Array Length, Number of threads

  • Output
  • The minimum value and Execution time.

Centre for Development of Advanced Computing