From 89afc3f322b53e3fc8e361b76050c63152258319 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 22 Dec 2021 09:17:48 +0000 Subject: [PATCH] Experiment With Multiple Threads --- src/list.c | 25 +++++++++++++++++++++++-- src/list.h | 9 ++++++++- src/test/test.c | 21 +++++++++++++-------- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/list.c b/src/list.c index f314153..51b0d26 100644 --- a/src/list.c +++ b/src/list.c @@ -21,6 +21,7 @@ typedef struct list { int *_array; int _currentSize; int _maxSize; + pthread_mutex_t simpleMutex; } List; int errorCodes[ERRNO_SIZE] = { @@ -31,8 +32,13 @@ int errorCodes[ERRNO_SIZE] = { ERRNO_005 }; -List *newList() { - return List_createList(10, -1); +List *List_new() { + List *list = List_createList(10, -1); + if (!list) { + return NULL; + } + pthread_mutex_init(&list->simpleMutex, NULL); + return list; } int List_get(List *list, int index) { @@ -49,6 +55,10 @@ int List_append(List *list, int element) { } } + // Beginning of Critical Section + pthread_mutex_lock(&(list->simpleMutex)); + printf("\nMutex Locked!"); + if (list->_currentSize + 1 == list->_maxSize) { list->_maxSize *= 2; int *temp = realloc(list->_array, list->_maxSize * sizeof(int)); @@ -59,6 +69,10 @@ int List_append(List *list, int element) { } list->_array[++list->_currentSize] = element; + // End of Critical Section + pthread_mutex_unlock(&(list->simpleMutex)); + printf("\nMutex Unlocked!"); + return 0; } @@ -190,6 +204,10 @@ int List_length(List *list) { } void List_print(List *list) { + + // Beginning Of Critical Section + pthread_mutex_lock(&(list->simpleMutex)); + printf("["); for (int i = 0; i < list->_currentSize + 1; i++) { if (i == list->_currentSize) { @@ -199,6 +217,9 @@ void List_print(List *list) { } } printf("]"); + + // End Of Critical Section + pthread_mutex_unlock(&(list->simpleMutex)); } void List_destroy(List **list) { diff --git a/src/list.h b/src/list.h index 886b6ef..095214d 100644 --- a/src/list.h +++ b/src/list.h @@ -11,7 +11,14 @@ typedef struct list List; -List *newList(); +/* + * Function: List_new + * ---------------------------- + * Return a new list that has been dynamically allocated memory on the heap + * + * returns: int (Pointer To List) + */ +List *List_new(); /* * Function: List_get diff --git a/src/test/test.c b/src/test/test.c index 1195c9b..54e73de 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1,3 +1,4 @@ +#include #include "src/list.h" #include "pthread.h" #include "string.h" @@ -14,7 +15,7 @@ void printSuccess(const char method[]) { void shouldReturnListLengthZero() { // Arrange - List *list = newList(); + List *list = List_new(); // Act -> Assert assert(List_length(list) == 0); @@ -25,7 +26,7 @@ void shouldReturnListLengthZero() { void shouldReturnListLengthTwo() { // Arrange - List *list = newList(); + List *list = List_new(); List_append(list, 1); List_append(list, 2); @@ -38,7 +39,7 @@ void shouldReturnListLengthTwo() { void shouldReturnSortedList() { // Arrange - List *list = newList(); + List *list = List_new(); List_append(list, 10); List_append(list, 9); List_append(list, 8); @@ -80,19 +81,23 @@ int main() { // shouldReturnListLengthZero(); // shouldReturnListLengthTwo(); // shouldReturnSortedList(); - List *list = newList(); - pthread_t pthreads[5]; - for (int i = 0; i < 5; i++) { + List *list = List_new(); + + pthread_t pthreads[2]; + for (int i = 0; i < 2; i++) { + printf("\nElement To Insert -> %i", i); Arguments args = {list, i}; pthread_create(&pthreads[i], NULL, append, &args); } - for (int i = 0; i < 5; i++) { - List_print(list); + for (int i = 0; i < 2; i++) { pthread_join(pthreads[i], NULL); } + printf("\n\nList: \n"); + List_print(list); + return 0; }