Experiment With Multiple Threads

main
sgoudham 3 years ago
parent 08a0ee0f1a
commit 89afc3f322

@ -21,6 +21,7 @@ typedef struct list {
int *_array; int *_array;
int _currentSize; int _currentSize;
int _maxSize; int _maxSize;
pthread_mutex_t simpleMutex;
} List; } List;
int errorCodes[ERRNO_SIZE] = { int errorCodes[ERRNO_SIZE] = {
@ -31,8 +32,13 @@ int errorCodes[ERRNO_SIZE] = {
ERRNO_005 ERRNO_005
}; };
List *newList() { List *List_new() {
return List_createList(10, -1); 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) { 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) { if (list->_currentSize + 1 == list->_maxSize) {
list->_maxSize *= 2; list->_maxSize *= 2;
int *temp = realloc(list->_array, list->_maxSize * sizeof(int)); 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; list->_array[++list->_currentSize] = element;
// End of Critical Section
pthread_mutex_unlock(&(list->simpleMutex));
printf("\nMutex Unlocked!");
return 0; return 0;
} }
@ -190,6 +204,10 @@ int List_length(List *list) {
} }
void List_print(List *list) { void List_print(List *list) {
// Beginning Of Critical Section
pthread_mutex_lock(&(list->simpleMutex));
printf("["); printf("[");
for (int i = 0; i < list->_currentSize + 1; i++) { for (int i = 0; i < list->_currentSize + 1; i++) {
if (i == list->_currentSize) { if (i == list->_currentSize) {
@ -199,6 +217,9 @@ void List_print(List *list) {
} }
} }
printf("]"); printf("]");
// End Of Critical Section
pthread_mutex_unlock(&(list->simpleMutex));
} }
void List_destroy(List **list) { void List_destroy(List **list) {

@ -11,7 +11,14 @@
typedef struct list List; 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 * Function: List_get

@ -1,3 +1,4 @@
#include <unistd.h>
#include "src/list.h" #include "src/list.h"
#include "pthread.h" #include "pthread.h"
#include "string.h" #include "string.h"
@ -14,7 +15,7 @@ void printSuccess(const char method[]) {
void shouldReturnListLengthZero() { void shouldReturnListLengthZero() {
// Arrange // Arrange
List *list = newList(); List *list = List_new();
// Act -> Assert // Act -> Assert
assert(List_length(list) == 0); assert(List_length(list) == 0);
@ -25,7 +26,7 @@ void shouldReturnListLengthZero() {
void shouldReturnListLengthTwo() { void shouldReturnListLengthTwo() {
// Arrange // Arrange
List *list = newList(); List *list = List_new();
List_append(list, 1); List_append(list, 1);
List_append(list, 2); List_append(list, 2);
@ -38,7 +39,7 @@ void shouldReturnListLengthTwo() {
void shouldReturnSortedList() { void shouldReturnSortedList() {
// Arrange // Arrange
List *list = newList(); List *list = List_new();
List_append(list, 10); List_append(list, 10);
List_append(list, 9); List_append(list, 9);
List_append(list, 8); List_append(list, 8);
@ -80,19 +81,23 @@ int main() {
// shouldReturnListLengthZero(); // shouldReturnListLengthZero();
// shouldReturnListLengthTwo(); // shouldReturnListLengthTwo();
// shouldReturnSortedList(); // shouldReturnSortedList();
List *list = newList();
pthread_t pthreads[5]; List *list = List_new();
for (int i = 0; i < 5; i++) {
pthread_t pthreads[2];
for (int i = 0; i < 2; i++) {
printf("\nElement To Insert -> %i", i);
Arguments args = {list, i}; Arguments args = {list, i};
pthread_create(&pthreads[i], NULL, append, &args); pthread_create(&pthreads[i], NULL, append, &args);
} }
for (int i = 0; i < 5; i++) { for (int i = 0; i < 2; i++) {
List_print(list);
pthread_join(pthreads[i], NULL); pthread_join(pthreads[i], NULL);
} }
printf("\n\nList: \n");
List_print(list);
return 0; return 0;
} }

Loading…
Cancel
Save