1. Introduction

    System Information

  2. Inter-Process Communication
  3. Signals

    Programming in Various Environments

  4. Script Programming
    1. Script Variables
    2. Test Conditions
    3. Control and Iteration
    4. Commonly used Programs
    5. Shell Capabilities
    6. Example looping script
    7. Example using Variables
    8. Example working with files
    9. Example install script
  5. C and C++ Programming
    1. POSIX System Capabilities
    2. More POSIX
    3. Threads
    4. Mutexes
    5. An example viewmod program
    6. An example serial program
  6. X Programming
  7. Debugging
  8. Credits

Linux Mutexes

Mutexes are a method used to be sure two threads, including the parent thread, do not attempt to access memory at the same time. One may logically thing that in order to send data between two threads, the parent thread may have a data structure with a buffer and a read and write counter. The child thread is getting data and putting it in the buffer. When the child thread gets data, it fills the buffer, then updates its counter. The parent thread can simply monitor the buffer counter values to tell when data is ready. Right? No, wrong! Not all systems will ensure that memory is written to buffers in the order the program may write them. Therefore it is necessary to use a locking mechanism to be sure data is always read and written correctly when communicating between threads. Mutexes are one way to do this. Mutexes ensure that memory accesses to the same memory are mutually exclusive.

Mutex Structure and Mutex Functions

  • pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  • int pthread_mutex_init (pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
  • int pthread_mutex_destroy (pthread_mutex_t *mutex);
  • int pthread_mutex_lock (pthread_mutex_t *mutex); - Used to lock a mutex. This function will block until the mutex is unlocked, if it is locked. Do not try to lock the mutex if the same thread already has locked the mutex.
  • int pthread_mutex_trylock (pthread_mutex_t *mutex); - Returns an error code if the mutex is already locked by another process. This function will not block.
  • int pthread_mutex_unlock (pthread_mutex_t *mutex);

Do not make a copy of a mutex. An example of a mutex declaration:

typedef struct input_data
   pthread_mutex_t mutex;  //Controls access to sharedint
   int sharedint;
} input_data;

input_data datain = {PTHREAD_MUTEX_INITIALIZER, 0};

Condition Variables

Condition variables are used to convey information about the state of shared data which is associated with a mutex. When a thread is waiting on a condition variable, it is in blocking mode.

  • pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
  • int pthread_cond_init (pthread_cond_t *cond, pthread_condattr_t *condattr);
  • int pthread_cond_destroy (pthread_cond_t *cond);
  • int pthread_cond_wait (pthread_cond_t *condition, pthread_mutex_t *mutex);
  • int pthread_cond_timedwait (pthread_cond_t *condition, pthread_mutex_t *mutex, struct timespec *expiration);
  • int pthread_cond_signal (pthread_cond_t *condition);
  • int pthread_cond_broadcast (pthread_cond_t *condition);

Do not make a copy of a condition variable. An example of a condition variable declaration:

typedef struct input_data
   pthread_mutex_t mutex;  //Controls access to sharedint
   pthread_cond_t condition;  //Indicates a change to sharedint
   int sharedint;
} input_data;