Experiment With Multiple Threads

main
sgoudham 3 years ago
parent 08a0ee0f1a
commit 89afc3f322

@ -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) {

@ -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

@ -1,3 +1,4 @@
#include <unistd.h>
#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;
}

Loading…
Cancel
Save