-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add concurrent queue with headlock and taillock.
- Loading branch information
1 parent
fe595a4
commit 73fa62f
Showing
12 changed files
with
100 additions
and
22 deletions.
There are no files selected for viewing
10 changes: 5 additions & 5 deletions
10
Homework9-threads-and-locks/Chap29-threads-and-locks-usage/.idea/workspace.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-476 Bytes
(91%)
Homework9-threads-and-locks/Chap29-threads-and-locks-usage/cmake-build-debug/.ninja_deps
Binary file not shown.
9 changes: 3 additions & 6 deletions
9
Homework9-threads-and-locks/Chap29-threads-and-locks-usage/cmake-build-debug/.ninja_log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
# ninja log v5 | ||
3 137 6909846641506430 CMakeFiles/Chap29_threads_and_locks_usage.dir/simpleCounter.cpp.obj 19fd7cb9b8b7a1ef | ||
130 313 6909878043212760 Chap29_threads_and_locks_usage.exe 28d5f7788f6f412a | ||
127 321 6909888759960005 Chap29_threads_and_locks_usage.exe fd72925bcd311297 | ||
4 231 6909864474994750 CMakeFiles/Chap29_threads_and_locks_usage.dir/sloppyCounter.cpp.obj 6164b80443e74164 | ||
3 129 6909878041412755 CMakeFiles/Chap29_threads_and_locks_usage.dir/concurrentLinkedList.cpp.obj d283c3583ac387bf | ||
3 123 6909888067633143 CMakeFiles/Chap29_threads_and_locks_usage.dir/simpleQueue.cpp.obj c887e79ed2379aa9 | ||
3 186 6909888281624927 Chap29_threads_and_locks_usage.exe fd72925bcd311297 | ||
2 126 6909888524866510 CMakeFiles/Chap29_threads_and_locks_usage.dir/simpleQueue.cpp.obj c887e79ed2379aa9 | ||
127 316 6909888526716185 Chap29_threads_and_locks_usage.exe fd72925bcd311297 | ||
3 126 6909888758049703 CMakeFiles/Chap29_threads_and_locks_usage.dir/simpleQueue.cpp.obj c887e79ed2379aa9 | ||
127 321 6909888759960005 Chap29_threads_and_locks_usage.exe fd72925bcd311297 | ||
0 119 6909890717483448 CMakeFiles/Chap29_threads_and_locks_usage.dir/concurrentQueue.cpp.obj 96494427f563030a | ||
120 303 6909890719283800 Chap29_threads_and_locks_usage.exe 49e3923c251e7dd3 |
Binary file added
BIN
+10.1 KB
...e/cmake-build-debug/CMakeFiles/Chap29_threads_and_locks_usage.dir/concurrentQueue.cpp.obj
Binary file not shown.
Binary file modified
BIN
+20 Bytes
(100%)
...locks/Chap29-threads-and-locks-usage/cmake-build-debug/Chap29_threads_and_locks_usage.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions
4
...and-locks/Chap29-threads-and-locks-usage/cmake-build-debug/Testing/Temporary/LastTest.log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Start testing: Nov 24 02:27 Pacific Standard Time | ||
Start testing: Nov 24 02:31 Pacific Standard Time | ||
---------------------------------------------------------- | ||
End testing: Nov 24 02:27 Pacific Standard Time | ||
End testing: Nov 24 02:31 Pacific Standard Time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
Homework9-threads-and-locks/Chap29-threads-and-locks-usage/concurrentQueue.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// Created by tobyf on 11/24/2022. | ||
// | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <pthread.h> | ||
|
||
typedef struct __node_t { | ||
int key; | ||
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 = static_cast<node_t *>(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 key) { | ||
node_t *tmp = static_cast<node_t *>(malloc(sizeof(node_t))); | ||
tmp->key = key; | ||
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) { | ||
pthread_mutex_lock(&Q->headLock); | ||
node_t *tmp = Q->head; | ||
node_t *newHead = tmp->next; | ||
if (newHead == NULL) { | ||
perror("Queue was empty"); | ||
pthread_mutex_unlock(&Q->headLock); | ||
return -1; | ||
} | ||
newHead = newHead->next; | ||
Q->head = newHead; | ||
pthread_mutex_unlock(&Q->headLock); | ||
free(tmp); | ||
return 0; | ||
} | ||
|
||
void Queue_Print(queue_t *Q) { | ||
node_t *cur = Q->head; | ||
while (cur) { | ||
printf("%d\n", cur->key); | ||
cur = cur->next; | ||
} | ||
} | ||
|
||
int main() { | ||
queue_t *Q = static_cast<queue_t *>(malloc(sizeof(queue_t))); | ||
Queue_Init(Q); | ||
Queue_Enqueue(Q, 0); | ||
Queue_Enqueue(Q, 1); | ||
Queue_Enqueue(Q, 2); | ||
Queue_Enqueue(Q, 3); | ||
Queue_Dequeue(Q); | ||
Queue_Print(Q); | ||
printf("\n"); | ||
Queue_Enqueue(Q, 4); | ||
Queue_Enqueue(Q, 5); | ||
Queue_Dequeue(Q); | ||
Queue_Print(Q); | ||
pthread_mutex_destroy(&Q->headLock); | ||
pthread_mutex_destroy(&Q->tailLock); | ||
free(Q); | ||
return 0; | ||
} |