Given the following code, how would you make it thread-safe? // basic node structure typedef struct __node_t { int key; struct __node_t *next; } node_t; // basic list structure (one used per list)...



Given the following code, how would you make it thread-safe?



// basic node structure
typedef struct __node_t {
    int key;
    struct __node_t *next;
} node_t;


// basic list structure (one used per list)
typedef struct __list_t {
    node_t *head;


} list_t;


void List_Init(list_t *L) {
    L->head = NULL;

}


int List_Insert(list_t *L, int key) {


    node_t *new = malloc(sizeof(node_t));


    if (new == NULL) {
        perror("malloc");
        return -1; // fail
    }


    new->key = key;
    new->next = L->head;
    L->head = new;

    return 0; // success
  }


int List_Lookup(list_t *L, int key) {
    node_t *curr = L->head;
    while (curr) {
        if (curr->key == key) {
            return 0; // success
        }
    curr = curr->next;
    }


}




Group of answer choices


A. list_t is already thread-safe and does not need any locks in any of its operations.




B. Add a lock to the list_t struct, initialize the lock in the List_Init, and then any operation that involves a list_t, lock and unlock the entire function to ensure we do not have any data races.




Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here