Skip to content

Commit

Permalink
add concurrent queue with headlock and taillock.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyatgithub committed Nov 24, 2022
1 parent fe595a4 commit 73fa62f
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 22 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ project(Chap29_threads_and_locks_usage)

set(CMAKE_CXX_STANDARD 17)

#add_executable(Chap29_threads_and_locks_usage main.cpp simpleCounter.cpp sloppyCounter.cpp concurrentLinkedList.cpp )
add_executable(Chap29_threads_and_locks_usage simpleQueue.cpp)
#add_executable(Chap29_threads_and_locks_usage main.cpp simpleCounter.cpp sloppyCounter.cpp concurrentLinkedList.cpp simpleQueue.cpp )
add_executable(Chap29_threads_and_locks_usage concurrentQueue.cpp)
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
"directoryIndex" : 0,
"id" : "Chap29_threads_and_locks_usage::@6890427a1f51a3e7e1df",
"jsonFile" : "target-Chap29_threads_and_locks_usage-Debug-2634ef5ce22d2a5f6f90.json",
"jsonFile" : "target-Chap29_threads_and_locks_usage-Debug-8e0e0fe07dbd20a37c12.json",
"name" : "Chap29_threads_and_locks_usage",
"projectIndex" : 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"objects" :
[
{
"jsonFile" : "codemodel-v2-1dcec3b5a17d9357c6be.json",
"jsonFile" : "codemodel-v2-39427a57604ad9237e88.json",
"kind" : "codemodel",
"version" :
{
Expand Down Expand Up @@ -86,7 +86,7 @@
},
"codemodel-v2" :
{
"jsonFile" : "codemodel-v2-1dcec3b5a17d9357c6be.json",
"jsonFile" : "codemodel-v2-39427a57604ad9237e88.json",
"kind" : "codemodel",
"version" :
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
{
"backtrace" : 1,
"compileGroupIndex" : 0,
"path" : "simpleQueue.cpp",
"path" : "concurrentQueue.cpp",
"sourceGroupIndex" : 0
}
],
Expand Down
Binary file not shown.
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 not shown.
Binary file not shown.
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ cmake_ninja_workdir = C$:/Users/tobyf/Documents/cs5600_computerSystem_fall2022/H

build cmake_object_order_depends_target_Chap29_threads_and_locks_usage: phony || CMakeFiles/Chap29_threads_and_locks_usage.dir

build CMakeFiles/Chap29_threads_and_locks_usage.dir/simpleQueue.cpp.obj: CXX_COMPILER__Chap29_threads_and_locks_usage_Debug C$:/Users/tobyf/Documents/cs5600_computerSystem_fall2022/Homework9-threads-and-locks/Chap29-threads-and-locks-usage/simpleQueue.cpp || cmake_object_order_depends_target_Chap29_threads_and_locks_usage
DEP_FILE = CMakeFiles\Chap29_threads_and_locks_usage.dir\simpleQueue.cpp.obj.d
build CMakeFiles/Chap29_threads_and_locks_usage.dir/concurrentQueue.cpp.obj: CXX_COMPILER__Chap29_threads_and_locks_usage_Debug C$:/Users/tobyf/Documents/cs5600_computerSystem_fall2022/Homework9-threads-and-locks/Chap29-threads-and-locks-usage/concurrentQueue.cpp || cmake_object_order_depends_target_Chap29_threads_and_locks_usage
DEP_FILE = CMakeFiles\Chap29_threads_and_locks_usage.dir\concurrentQueue.cpp.obj.d
FLAGS = -g
OBJECT_DIR = CMakeFiles\Chap29_threads_and_locks_usage.dir
OBJECT_FILE_DIR = CMakeFiles\Chap29_threads_and_locks_usage.dir
Expand All @@ -63,7 +63,7 @@ build CMakeFiles/Chap29_threads_and_locks_usage.dir/simpleQueue.cpp.obj: CXX_COM
#############################################
# Link the executable Chap29_threads_and_locks_usage.exe

build Chap29_threads_and_locks_usage.exe: CXX_EXECUTABLE_LINKER__Chap29_threads_and_locks_usage_Debug CMakeFiles/Chap29_threads_and_locks_usage.dir/simpleQueue.cpp.obj
build Chap29_threads_and_locks_usage.exe: CXX_EXECUTABLE_LINKER__Chap29_threads_and_locks_usage_Debug CMakeFiles/Chap29_threads_and_locks_usage.dir/concurrentQueue.cpp.obj
FLAGS = -g
LINK_LIBRARIES = -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
OBJECT_DIR = CMakeFiles\Chap29_threads_and_locks_usage.dir
Expand Down
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;
}

0 comments on commit 73fa62f

Please sign in to comment.