From 7e229bd28f5aada24a2e5cfdb842e1bdc4e9c9c8 Mon Sep 17 00:00:00 2001 From: Michael _377 Date: Wed, 15 Apr 2026 11:51:30 +0800 Subject: [PATCH 1/7] Add files via upload --- hash_lock.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ list_lock.c | 51 ++++++++++++++++++++++++++++++ lock.c | 23 ++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 hash_lock.c create mode 100644 list_lock.c create mode 100644 lock.c diff --git a/hash_lock.c b/hash_lock.c new file mode 100644 index 0000000..80d1fd2 --- /dev/null +++ b/hash_lock.c @@ -0,0 +1,89 @@ +#include "hash_lock.h" + +#include +#include + +void hashInit(hash_lock_t* bucket) +{ + for(int i = 0;itable[i].head = NULL; + pthread_mutex_init(&bucket->table[i].mutex,NULL); + } +} +int getValue(hash_lock_t* bucket, int key) +{ + int which = HASH(key); + pthread_mutex_lock(&bucket->table[which].mutex); + Hlist p = bucket->table[which].head; //.h中规定的节点指针,指向链表的头节点 + while(NULL != p) + { + if(p->key == key) + { + pthread_mutex_unlock(&bucket->table[which].mutex); + return p->value; + } + p = p->next; + } + pthread_mutex_unlock(&bucket->table[which].mutex); + return -1; + +} + +void insert(hash_lock_t* bucket, int key, int value) +{ + int which = HASH(key); + pthread_mutex_lock(&bucket->table[which].mutex); + Hlist p = bucket->table[which].head; + while(NULL != p) + { + if(p->key == key) + { + p->value = value; + pthread_mutex_unlock(&bucket->table[which].mutex); + return; + } + p=p->next; + } + Hlist NewNode = (Hlist)malloc(sizeof(Hnode)); + NewNode->key = key; + NewNode->value = value; + NewNode->next = bucket->table[which].head; + bucket->table[which].head = NewNode; + pthread_mutex_unlock(&bucket->table[which].mutex); + return; + +} + +int setKey(hash_lock_t* bucket, int key, int new_key) +{ + int old_which = HASH(key); + int new_which = HASH(new_key); + pthread_mutex_lock(&bucket->table[old_which].mutex); + Hlist p = bucket->table[old_which].head; + Hlist former = NULL;//记录前面一个 + while(NULL!=p) + { + if(p->key == key) + { + if(former == NULL) + { + bucket->table[old_which].head = p->next; + } + else + { + former->next = p->next; + } + p->key = new_key; + p->next = bucket->table[new_which].head; + bucket->table[new_which].head = p; + + pthread_mutex_unlock(&bucket->table[old_which].mutex); + return 0; + } + former = p; + p = p->next; + } + pthread_mutex_unlock(&bucket->table[old_which].mutex); + return -1; +} \ No newline at end of file diff --git a/list_lock.c b/list_lock.c new file mode 100644 index 0000000..f9ce911 --- /dev/null +++ b/list_lock.c @@ -0,0 +1,51 @@ +#include "list_lock.h" + +#include +#include + +void listInit(list_lock_t* list) +{ + list->head = NULL; + pthread_mutex_init(&list->mutex,NULL); + pthread_cond_init(&list->cond,NULL); + +} + +void producer(list_lock_t* list, DataType value) +{ + pthread_mutex_lock(&list->mutex);//加锁 + LNode *Newnode = (LNode*)malloc(sizeof(LNode));//创建新节点 + Newnode->value = value;//给节点赋值 + Newnode->next = list->head;//头插法 + list->head = Newnode; + pthread_mutex_unlock(&list->mutex);//解锁 + pthread_cond_signal(&list->cond);//唤醒 +} + +void consumer(list_lock_t* list) +{ + pthread_mutex_lock(&list->mutex);//加锁 + while(list->head == NULL)//看看链表是不是空的,空的话就睡觉i + { + pthread_cond_wait(&list->cond,&list->mutex);//用conditional Variable来睡觉-->生产者的cond_signal会拍醒消费者 + } + LNode *CunChu = list->head; + list->head = CunChu->next; + free(CunChu); + pthread_mutex_unlock(&list->mutex); + +} + +int getListSize(list_lock_t* list) +{ + pthread_mutex_lock(&list->mutex);//加锁 + int count = 0;//计数 + LNode *p = list->head;//指向头链表,开始走,走一个数一个 + while(p != NULL) + { + count++; + p = p->next; + } + pthread_mutex_unlock(&list->mutex);//解锁 + return count; +} \ No newline at end of file diff --git a/lock.c b/lock.c new file mode 100644 index 0000000..28e512c --- /dev/null +++ b/lock.c @@ -0,0 +1,23 @@ +#include "lock.h" + +#include + +void amountInit(lock_t* Account) +{ + Account->amount = 0; + pthread_mutex_init(&Account->mutex, NULL); +} + +void Income(lock_t* Account, int amount) +{ + pthread_mutex_lock(&Account->mutex); + Account->amount += amount; + pthread_mutex_unlock(&Account->mutex); +} + +void Expend(lock_t* Account, int amount) +{ + pthread_mutex_lock(&Account->mutex); + Account->amount -= amount; + pthread_mutex_unlock(&Account->mutex); +} \ No newline at end of file From 186c60cb4e42c65e213e3d57ea6c9a0435b7c2ba Mon Sep 17 00:00:00 2001 From: Michael _377 Date: Wed, 15 Apr 2026 11:52:01 +0800 Subject: [PATCH 2/7] Delete hash_lock.c --- hash_lock.c | 89 ----------------------------------------------------- 1 file changed, 89 deletions(-) delete mode 100644 hash_lock.c diff --git a/hash_lock.c b/hash_lock.c deleted file mode 100644 index 80d1fd2..0000000 --- a/hash_lock.c +++ /dev/null @@ -1,89 +0,0 @@ -#include "hash_lock.h" - -#include -#include - -void hashInit(hash_lock_t* bucket) -{ - for(int i = 0;itable[i].head = NULL; - pthread_mutex_init(&bucket->table[i].mutex,NULL); - } -} -int getValue(hash_lock_t* bucket, int key) -{ - int which = HASH(key); - pthread_mutex_lock(&bucket->table[which].mutex); - Hlist p = bucket->table[which].head; //.h中规定的节点指针,指向链表的头节点 - while(NULL != p) - { - if(p->key == key) - { - pthread_mutex_unlock(&bucket->table[which].mutex); - return p->value; - } - p = p->next; - } - pthread_mutex_unlock(&bucket->table[which].mutex); - return -1; - -} - -void insert(hash_lock_t* bucket, int key, int value) -{ - int which = HASH(key); - pthread_mutex_lock(&bucket->table[which].mutex); - Hlist p = bucket->table[which].head; - while(NULL != p) - { - if(p->key == key) - { - p->value = value; - pthread_mutex_unlock(&bucket->table[which].mutex); - return; - } - p=p->next; - } - Hlist NewNode = (Hlist)malloc(sizeof(Hnode)); - NewNode->key = key; - NewNode->value = value; - NewNode->next = bucket->table[which].head; - bucket->table[which].head = NewNode; - pthread_mutex_unlock(&bucket->table[which].mutex); - return; - -} - -int setKey(hash_lock_t* bucket, int key, int new_key) -{ - int old_which = HASH(key); - int new_which = HASH(new_key); - pthread_mutex_lock(&bucket->table[old_which].mutex); - Hlist p = bucket->table[old_which].head; - Hlist former = NULL;//记录前面一个 - while(NULL!=p) - { - if(p->key == key) - { - if(former == NULL) - { - bucket->table[old_which].head = p->next; - } - else - { - former->next = p->next; - } - p->key = new_key; - p->next = bucket->table[new_which].head; - bucket->table[new_which].head = p; - - pthread_mutex_unlock(&bucket->table[old_which].mutex); - return 0; - } - former = p; - p = p->next; - } - pthread_mutex_unlock(&bucket->table[old_which].mutex); - return -1; -} \ No newline at end of file From 5a6b49940ca4d391fb5ccb800689a32680f1602a Mon Sep 17 00:00:00 2001 From: Michael _377 Date: Wed, 15 Apr 2026 11:52:12 +0800 Subject: [PATCH 3/7] Delete list_lock.c --- list_lock.c | 51 --------------------------------------------------- 1 file changed, 51 deletions(-) delete mode 100644 list_lock.c diff --git a/list_lock.c b/list_lock.c deleted file mode 100644 index f9ce911..0000000 --- a/list_lock.c +++ /dev/null @@ -1,51 +0,0 @@ -#include "list_lock.h" - -#include -#include - -void listInit(list_lock_t* list) -{ - list->head = NULL; - pthread_mutex_init(&list->mutex,NULL); - pthread_cond_init(&list->cond,NULL); - -} - -void producer(list_lock_t* list, DataType value) -{ - pthread_mutex_lock(&list->mutex);//加锁 - LNode *Newnode = (LNode*)malloc(sizeof(LNode));//创建新节点 - Newnode->value = value;//给节点赋值 - Newnode->next = list->head;//头插法 - list->head = Newnode; - pthread_mutex_unlock(&list->mutex);//解锁 - pthread_cond_signal(&list->cond);//唤醒 -} - -void consumer(list_lock_t* list) -{ - pthread_mutex_lock(&list->mutex);//加锁 - while(list->head == NULL)//看看链表是不是空的,空的话就睡觉i - { - pthread_cond_wait(&list->cond,&list->mutex);//用conditional Variable来睡觉-->生产者的cond_signal会拍醒消费者 - } - LNode *CunChu = list->head; - list->head = CunChu->next; - free(CunChu); - pthread_mutex_unlock(&list->mutex); - -} - -int getListSize(list_lock_t* list) -{ - pthread_mutex_lock(&list->mutex);//加锁 - int count = 0;//计数 - LNode *p = list->head;//指向头链表,开始走,走一个数一个 - while(p != NULL) - { - count++; - p = p->next; - } - pthread_mutex_unlock(&list->mutex);//解锁 - return count; -} \ No newline at end of file From 927ca1cde9a3e5a4f9c4da7a9c53c0d20e356aed Mon Sep 17 00:00:00 2001 From: Michael _377 Date: Wed, 15 Apr 2026 11:52:21 +0800 Subject: [PATCH 4/7] Delete lock.c --- lock.c | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 lock.c diff --git a/lock.c b/lock.c deleted file mode 100644 index 28e512c..0000000 --- a/lock.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "lock.h" - -#include - -void amountInit(lock_t* Account) -{ - Account->amount = 0; - pthread_mutex_init(&Account->mutex, NULL); -} - -void Income(lock_t* Account, int amount) -{ - pthread_mutex_lock(&Account->mutex); - Account->amount += amount; - pthread_mutex_unlock(&Account->mutex); -} - -void Expend(lock_t* Account, int amount) -{ - pthread_mutex_lock(&Account->mutex); - Account->amount -= amount; - pthread_mutex_unlock(&Account->mutex); -} \ No newline at end of file From e5783990ba1f08f8c4f00dcf778dcb88baff51e0 Mon Sep 17 00:00:00 2001 From: Michael _377 Date: Wed, 15 Apr 2026 11:58:30 +0800 Subject: [PATCH 5/7] Update lock.c --- src/lock/lock.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/lock/lock.c b/src/lock/lock.c index f65675f..15e1aee 100644 --- a/src/lock/lock.c +++ b/src/lock/lock.c @@ -2,14 +2,22 @@ #include -void amountInit(lock_t* Account) { - perror("This function is not implemented"); +void amountInit(lock_t* Account) +{ + Account->amount = 0; + pthread_mutex_init(&Account->mutex, NULL); } -void Income(lock_t* Account, int amount) { - perror("This function is not implemented"); +void Income(lock_t* Account, int amount) +{ + pthread_mutex_lock(&Account->mutex); + Account->amount += amount; + pthread_mutex_unlock(&Account->mutex); } -void Expend(lock_t* Account, int amount) { - perror("This function is not implemented"); -} \ No newline at end of file +void Expend(lock_t* Account, int amount) +{ + pthread_mutex_lock(&Account->mutex); + Account->amount -= amount; + pthread_mutex_unlock(&Account->mutex); +} From 8380bf980675d74c8af7733949e4bf54018784a1 Mon Sep 17 00:00:00 2001 From: Michael _377 Date: Wed, 15 Apr 2026 11:59:18 +0800 Subject: [PATCH 6/7] Update list_lock.c --- src/lock/list_lock.c | 49 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/src/lock/list_lock.c b/src/lock/list_lock.c index 51b13c3..b97fd8f 100644 --- a/src/lock/list_lock.c +++ b/src/lock/list_lock.c @@ -3,18 +3,49 @@ #include #include -void listInit(list_lock_t* list) { - perror("This function is not implemented"); +void listInit(list_lock_t* list) +{ + list->head = NULL; + pthread_mutex_init(&list->mutex,NULL); + pthread_cond_init(&list->cond,NULL); + } -void producer(list_lock_t* list, DataType value) { - perror("This function is not implemented"); +void producer(list_lock_t* list, DataType value) +{ + pthread_mutex_lock(&list->mutex);//加锁 + LNode *Newnode = (LNode*)malloc(sizeof(LNode));//创建新节点 + Newnode->value = value;//给节点赋值 + Newnode->next = list->head;//头插法 + list->head = Newnode; + pthread_mutex_unlock(&list->mutex);//解锁 + pthread_cond_signal(&list->cond);//唤醒 } -void consumer(list_lock_t* list) { - perror("This function is not implemented"); +void consumer(list_lock_t* list) +{ + pthread_mutex_lock(&list->mutex);//加锁 + while(list->head == NULL)//看看链表是不是空的,空的话就睡觉i + { + pthread_cond_wait(&list->cond,&list->mutex);//用conditional Variable来睡觉-->生产者的cond_signal会拍醒消费者 + } + LNode *CunChu = list->head; + list->head = CunChu->next; + free(CunChu); + pthread_mutex_unlock(&list->mutex); + } -int getListSize(list_lock_t* list) { - perror("This function is not implemented"); -} \ No newline at end of file +int getListSize(list_lock_t* list) +{ + pthread_mutex_lock(&list->mutex);//加锁 + int count = 0;//计数 + LNode *p = list->head;//指向头链表,开始走,走一个数一个 + while(p != NULL) + { + count++; + p = p->next; + } + pthread_mutex_unlock(&list->mutex);//解锁 + return count; +} From 742a8a683d6be631a0bcc48e655a38a90a4965d6 Mon Sep 17 00:00:00 2001 From: Michael _377 Date: Wed, 15 Apr 2026 11:59:52 +0800 Subject: [PATCH 7/7] Update hash_lock.c --- src/lock/hash_lock.c | 87 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 9 deletions(-) diff --git a/src/lock/hash_lock.c b/src/lock/hash_lock.c index 67113ac..a5bbb4b 100644 --- a/src/lock/hash_lock.c +++ b/src/lock/hash_lock.c @@ -3,18 +3,87 @@ #include #include -void hashInit(hash_lock_t* bucket) { - perror("This function is not implemented"); +void hashInit(hash_lock_t* bucket) +{ + for(int i = 0;itable[i].head = NULL; + pthread_mutex_init(&bucket->table[i].mutex,NULL); + } } +int getValue(hash_lock_t* bucket, int key) +{ + int which = HASH(key); + pthread_mutex_lock(&bucket->table[which].mutex); + Hlist p = bucket->table[which].head; //.h中规定的节点指针,指向链表的头节点 + while(NULL != p) + { + if(p->key == key) + { + pthread_mutex_unlock(&bucket->table[which].mutex); + return p->value; + } + p = p->next; + } + pthread_mutex_unlock(&bucket->table[which].mutex); + return -1; -int getValue(hash_lock_t* bucket, int key) { - perror("This function is not implemented"); } -void insert(hash_lock_t* bucket, int key, int value) { - perror("This function is not implemented"); +void insert(hash_lock_t* bucket, int key, int value) +{ + int which = HASH(key); + pthread_mutex_lock(&bucket->table[which].mutex); + Hlist p = bucket->table[which].head; + while(NULL != p) + { + if(p->key == key) + { + p->value = value; + pthread_mutex_unlock(&bucket->table[which].mutex); + return; + } + p=p->next; + } + Hlist NewNode = (Hlist)malloc(sizeof(Hnode)); + NewNode->key = key; + NewNode->value = value; + NewNode->next = bucket->table[which].head; + bucket->table[which].head = NewNode; + pthread_mutex_unlock(&bucket->table[which].mutex); + return; + } -int setKey(hash_lock_t* bucket, int key, int new_key) { - perror("This function is not implemented"); -} \ No newline at end of file +int setKey(hash_lock_t* bucket, int key, int new_key) +{ + int old_which = HASH(key); + int new_which = HASH(new_key); + pthread_mutex_lock(&bucket->table[old_which].mutex); + Hlist p = bucket->table[old_which].head; + Hlist former = NULL;//记录前面一个 + while(NULL!=p) + { + if(p->key == key) + { + if(former == NULL) + { + bucket->table[old_which].head = p->next; + } + else + { + former->next = p->next; + } + p->key = new_key; + p->next = bucket->table[new_which].head; + bucket->table[new_which].head = p; + + pthread_mutex_unlock(&bucket->table[old_which].mutex); + return 0; + } + former = p; + p = p->next; + } + pthread_mutex_unlock(&bucket->table[old_which].mutex); + return -1; +}