Exercise

Enhance your understanding and skills by attempting this exercise on the usage of thread API.

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) ...