Search⌘ K

Exercise

Explore the process of writing multi-threaded programs using the POSIX Thread API, and use the Helgrind tool to detect common concurrency issues such as data races and deadlocks. Understand how locks and condition variables affect program correctness and performance through practical exercises and code analysis.

We'll cover the following...

Simulator

In this section, we’ll write some simple multi-threaded programs and use a specific tool, called helgrind, to find problems in these programs.

Press run in the code widget below, you will get a terminal. Based on the questions given below, run the commands and make observations.

#include <stdio.h>

#include "mythreads.h"

int balance = 10;

void* worker(void* arg) {
    balance++; // unprotected access 
    return NULL;
}

int main(int argc, char *argv[]) {
    pthread_t p;
    Pthread_create(&p, NULL, worker, NULL);
    balance++; // unprotected access
    Pthread_join(p, NULL);
    return 0;
}

Questions

  1. First, build main-race.c by running make main-race. Examine the code so you can see the (hopefully obvious) ...