第29章基于锁的并发数据结构

29.1 并发计数器

如下为一个非并发的计数器:

 typedef struct counter_t {
	 int value;
 } counter_t;

 void init(counter_t *c) {
 c->value = 0;
 }

 void increment(counter_t *c) {
 c->value++;
 }
 
 void decrement(counter_t *c) {
 c->value--;
 }
 
 int get(counter_t *c) {
 return c->value;
 }

我们要做的一个挑战是將上述代码变得线程安全:

typedef struct counter_t {
	int value;
	pthread_mutex_t lock;
} counter_t;

void init(counter_t *c) {
	c->value = 0;
	Pthread_mutex_init(&c->lock,NULL);
}

void increment(counter_t *c) {
	Pthread_mutex_lock(&c->lock);
	c->value++;
	Pthread_mutex_unlock(&c->lock);
}

void decrement(counter_t *c) {
	Pthread_mutex_lock(&c->lock);
	c->value--;
	Pthread_mutex_unlock(&c->lock);
}

int get(counter_t *c) {
	Pthread_mutex_lock(&c->lock);
	int rc = c->value;
	Pthread_mutex_unlock(&c->lock);
	return rc;
}
29.2 并发链表
 // 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;
	 pthread_mutex_t lock;
 } list_t;

void List_Init(list_t *L) {
	 L->head = NULL;
	 pthread_mutex_init(&L->lock, NULL);
}

int List_Insert(list_t *L, int key) {
	pthread_mutex_lock(&L->lock);
	node_t *new = malloc(sizeof(node_t));
	if (new == NULL) {
		perror("malloc");
		pthread_mutex_unlock(&L->lock);
		return -1; // fail
	}
	new->key = key;
	new->next = L->head;
	L->head = new;
	pthread_mutex_unlock(&L->lock);
	return 0; // success
}

int List_Lookup(list_t *L, int key) {
	pthread_mutex_lock(&L->lock);
	node_t *curr = L->head;
	while (curr) {
		if (curr->key == key) {
			pthread_mutex_unlock(&L->lock);
			return 0; // success		
		}
		curr = curr->next;
	}
	pthread_mutex_unlock(&L->lock);
	return -1; // failure
}
29.3 并发队列
 typedef struct node_t {
	 int value;
	 struct node_t *next;
 } node_t;

 typedef struct queue_t {
	 node_t *head;
	 node_t *tail;
	 pthread_mutex_t headLock;
	 pthread_mutex_t tailLock;
 } queue_t;

 void Queue_Init(queue_t *q) {
	 node_t *tmp = malloc(sizeof(node_t));
	 tmp->next = NULL;
	 q->head = q->tail = tmp;
	 pthread_mutex_init(&q->headLock, NULL);
	 pthread_mutex_init(&q->tailLock, NULL);
 }

 void Queue_Enqueue(queue_t *q, int value) {
	 node_t *tmp = malloc(sizeof(node_t));
	 assert(tmp != NULL);
	 tmp->value = value;
	 tmp->next = NULL;
	 pthread_mutex_lock(&q->tailLock);
     q->tail->next = tmp;
	 q->tail = tmp;
	 pthread_mutex_unlock(&q->tailLock);
}

int Queue_Dequeue(queue_t *q, int *value) {
	pthread_mutex_lock(&q->headLock);
	node_t *tmp = q->head;
	node_t *newHead = tmp->next;
	if (newHead == NULL) {
		pthread_mutex_unlock(&q->headLock);
		return -1; // queue was empty
	}
	*value = newHead->value;
	q->head = newHead;
	pthread_mutex_unlock(&q->headLock);
	free(tmp);
	return 0;
}

你会发现有两个锁,一个负责队列头,另一个负责队列尾。这两个锁使得入队列操作和出队列操作可以并发执行,因为入队列只访问 tail 锁,而出队列只访问 head 锁。

29.4 并发散列表
#define BUCKETS (101)
typedef struct
	 hash_t {
   	 list_t lists[BUCKETS];
} hash_t;

void Hash_Init(hash_t *H) {
	int i;
	for (i = 0; i < BUCKETS; i++) {
		List_Init(&H->lists[i]);
	}
}

int Hash_Insert(hash_t *H, int key) {
	int bucket = key % BUCKETS;
	return List_Insert(&H->lists[bucket], key);
}

int Hash_Lookup(hash_t *H, int key) {
	int bucket = key % BUCKETS;
	return List_Lookup(&H->lists[bucket], key);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值