Warning
This page is located in archive.

Lab 08 - Threading Part 2

Trylock Example

Previously we covered pthread_mutex_lock and pthread_mutex_unlock. There is also a third locking mechanism: pthread_mutex_trylock. Try lock will not wait until the lock is available, it will return immediately. It will return zero if it acquired the lock, else something else if not (specifically an error code, most commonly a number meaning “Lock is busy”).

Exercise 1: Modify the code from last week. Start by adapting the code to use initialise and destroy the locks with pthread_mutex_init(&mutex, NULL); and pthread_mutex_destroy(&mutex);. Change the locks to use pthread_mutex_trylock instead of pthread_mutex_lock. If the lock cannot be acquired, wait and try again.

Producer-Consumer Model

Threads are obviously used where we want multiple things to happen at the same time. Therefore they are commonly employed for slow processes like in the following schemes:

  • 1 thread doing heavy calculation, 1 thread can interact with the user - threading means the program remains responsive to user input
  • 5 threads reading a file each, correcting spelling errors, and then saving the result - threading means all 5 can be done at the same time on different CPU cores, rather than one after the other
  • 15 producer threads reading reading data and placing it in buffers, and 3 consumer threads reading these buffers and doing something with it - threading means that we can have many producers ensuring the buffers are always full for the consumers, the consumers can easily check many buffers and have the data ready for them when they need it

The final layout is called a producer-consumer model. It can interact with many buffers in the following way:

if(pthread_mutex_trylock(&lockA) == 0)
{
    //lock was available, modify the data here
    pthread_mutex_unlock(&lockA);
}
else if(pthread_mutex_trylock(&lockB) == 0)
{
    //else, try store the data in another buffer
    pthread_mutex_unlock(&lockB);
}

But is this a good idea? Hint: What happens if no locks are available? Think about how we can solve this.

Exercise 2: Create 3 threads which will generate a random integer *once per second*. The threads will loop forever. These will be our producers. They will safely put the random number into one of two global buffers.

There will then be two other threads, our consumers, which will check if there is data in the buffers. If so, they will grab the result, and calculate if it is a prime number, and output the result.

Extension Tasks

  • Consider coding in such a way that we can easily change the number of producers/buffers/consumers with a single variable (each). ie. int nProducerThreads = 15;
  • Create a global variable that all threads will check regularly. If it is set to a specific value (called a sentinel), the threads will close. After creating the producers and consumers, the main thread can then wait for the user to press enter (scanf) before closing. When it closes, it will set the global variable to the sentinel so each thread will exit, join them all, and then return zero.
courses/be5b99cpl/labs/lab10.txt · Last modified: 2023/11/29 03:08 by brouggeo