线程安全链表在C中细粒度

时间:2015-06-13 18:18:24

标签: c multithreading linked-list

我正在实现一个带有细粒度锁定的链表,这意味着每个节点都有一个锁。

当我添加一个新节点时,我想只传递两个参数:key和value。 如何确保每个节点都有不同的锁?锁定由pthread_mutex_t实现。

这是我要添加的实现:

int setos_add(int key, void* value)
{
volatile setos_node* node = head;
volatile setos_node* prev;

pthread_mutex_t new_mutex;  //wrong: it will put the same lock to everyone

//lock the head node
if(pthread_mutex_lock(&(node->mutex)) == 0){
    printf("failed locking\n");
    exit(1);
}

// go through nodes until the key of "node" exceeds "key"
// new node should then be between "prev" and "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);
        }
    }
    if (node -> key < key){  //else: we need also preve staying locked for       the insertions
        if (pthread_mutex_unlock(&(prev->mutex)) != 0){
            printf("failed unlocking\n");
            exit(1);
        }
    }
}

// if node is same as our key - key already in list
if ((node != NULL) && (node->key == key)){

    if (node != NULL){  //not the end of the list
        if (pthread_mutex_unlock(&(node->mutex)) != 0){
            printf("failed unlocking\n");
            exit(1);
        }
    }

    if (prev != NULL){ //not the begining of the list
        if (pthread_mutex_unlock(&(prev->mutex)) != 0){
            printf("failed unlocking\n");
            exit(1);
        }
    }
    return 0;
}

// allocate new node and initialize it
setos_node* new_node = (setos_node*) malloc(sizeof(setos_node));
new_node->key = key;
new_node->value = value;
new_node->mutex = new_mutex;  //to change
if(pthread_mutex_init(&(new_node-> mutex), NULL) != 0){
    printf("failed init mutex");
    exit(1);
}


// place node into list
new_node->next = node;
prev->next = new_node;

if (node != NULL){ //not the end of the list
    if (pthread_mutex_unlock(&(node->mutex)) != 0){
        printf("failed unlocking\n");
        exit(1);
    }
}

if (prev != NULL){ //not the begining of the list
    if (pthread_mutex_unlock(&(prev->mutex)) != 0){
        printf("failed unlocking\n");
        exit(1);

    }
}

return 1;
}

你可以看到,在第三行我正在为节点定义pthread_mutex_t。但这样做,每个新节点是否都具有相同的pthread_mutex_t

我该怎么做?

1 个答案:

答案 0 :(得分:0)

是的,它将是相同的互斥锁。

每次需要新的互斥锁时,都可以使用malloc新的互斥锁元素:

malloc(sizeof(pthread_mutex_t)))

像这样的初始化:

pthread_mutex_init(m, NULL)