multithreading - threads safe linked list fine grained in C -
i'm implementing linked list fine grained locking, meaning lock in every node. when i'm adding new node, want pass 2 arguments: key , value. how can make sure each node has different lock? lock implemented pthread_mutex_t . this implementation add: int setos_add(int key, void* value) { volatile setos_node* node = head; volatile setos_node* prev; pthread_mutex_t new_mutex; //wrong: put same lock //lock head node if(pthread_mutex_lock(&(node->mutex)) == 0){ printf("failed locking\n"); exit(1); } // go through nodes until key of "node" exceeds "key" // new node should between "prev" , "node" while ((node != null) && (node->key < key)) { prev = node; //already locked node = node->next; //locking 2 nodes each time if (node != null){ if(pthread_mutex_lock(&(node->mutex)) == 0){ printf("failed locking\n"); exit(1); } ...