This is some code in C for quicksort. The quicksort works correctly, but I am trying to implement multithreading. I am trying to run the recursive calls in parallel with a limit on how much threads can be running at one time (set by the global variable 'maximumThreads'). My logic is incorrect with managing how many threads can be ran at the same time. The part that I need you to look at is after the for loop in quick sort, where I have my logic for the mutex and the conditional variable. Right now when I run my code, the program runs without stopping. I would like help with correctly implementing this. #include #include #include #include #define SORT_THRESHOLD 40 typedef struct _sortParams { char** array; int left; int right; int* currentThreads; pthread_mutex_t* mutex; pthread_cond_t* cond_var } SortParams; static int maximumThreads; /* maximum # of threads to be used */ /* This is an implementation of insert sort, which although it is */ /* n-squared, is faster at sorting short lists than quick sort, */ /* due to its lack of recursive procedure call overhead. */ static void insertSort(char** array, int left, int right) { int i, j; for (i = left + 1; i <= right; i++) { char* pivot = array[i]; j = i - 1; while (j >= left && (strcmp(array[j],pivot) > 0)) { array[j + 1] = array[j]; j--; } array[j + 1] = pivot; } } /* Recursive quick sort, but with a provision to use */ /* insert sort when the range gets small. */ static void quickSort(void* p) { SortParams* params = (SortParams*) p; char** array = params->array; int left = params->left; int right = params->right; int i = left, j = right; if (j - i > SORT_THRESHOLD) { /* if the sort range is substantial, use quick sort */ int m = (i + j) >> 1; /* pick pivot as median of */ char* temp, *pivot; /* first, last and middle elements */ if (strcmp(array[i],array[m]) > 0) { temp = array[i]; array[i] = array[m]; array[m] = temp; } if (strcmp(array[m],array[j]) > 0) { temp = array[m]; array[m] = array[j]; array[j] = temp; if (strcmp(array[i],array[m]) > 0) { temp = array[i]; array[i] = array[m]; array[m] = temp; } } pivot = array[m]; for (;;) { while (strcmp(array[i],pivot) < 0) i++; /* move i down to first element greater than or equal to pivot */ while (strcmp(array[j],pivot) > 0) j--; /* move j up to first element less than or equal to pivot */ if (i < j) { char* temp = array[i]; /* if i and j have not passed each other */ array[i++] = array[j]; /* swap their respective elements and */ array[j--] = temp; /* advance both i and j */ } else if (i == j) { i++; j--; } else break; /* if i > j, this partitioning is done */ } pthread_mutex_lock(params->mutex); // Wait if the maximum number of threads is reached while(*params->currentThreads >= maximumThreads) { pthread_cond_wait(params->cond_var, params->mutex); } // Increment the current thread count (*params->currentThreads)++; pthread_mutex_unlock(params->mutex); SortParams first = {params->array, left, j, params->currentThreads, params->mutex, params->cond_var}; SortParams second = {params->array, i, right, params->currentThreads, params->mutex, params->cond_var}; pthread_t thread1, thread2; pthread_create(&thread1, NULL, (void*)quickSort, &first); pthread_create(&thread2, NULL, (void*)quickSort, &second); pthread_join(thread1, NULL); pthread_join(thread2, NULL); // After the threads finish, decrement the current thread count // and signal any waiting threads pthread_mutex_lock(params->mutex); (*params->currentThreads)--; pthread_cond_signal(params->cond_var); pthread_mutex_unlock(params->mutex); } else insertSort(array,i,j); /* for a small range use insert sort */ } /* user interface routine to set the number of threads sortT is permitted to use */ void setSortThreads(int count) { maximumThreads = count; } /* user callable sort procedure, sorts array of count strings, beginning at address array */ void sortThreaded(char** array, unsigned int count) { SortParams parameters; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER; int currentThreads = 0; parameters.array = array; parameters.left = 0; parameters.right = count - 1; parameters.currentThreads = ¤tThreads; parameters.mutex = &mutex; parameters.cond_var = &cond_var; quickSort(¶meters); pthread_cond_destroy(&cond_var); pthread_mutex_destroy(&mutex); }
This is some code in C for quicksort. The quicksort works correctly, but I am trying to implement multithreading. I am trying to run the recursive calls in parallel with a limit on how much threads can be running at one time (set by the global variable 'maximumThreads'). My logic is incorrect with managing how many threads can be ran at the same time. The part that I need you to look at is after the for loop in quick sort, where I have my logic for the mutex and the conditional variable. Right now when I run my code, the program runs without stopping. I would like help with correctly implementing this.
#include <string.h>
#include <pthread.h>
#include <stdio.h>
#define SORT_THRESHOLD 40
typedef struct _sortParams {
char** array;
int left;
int right;
int* currentThreads;
pthread_mutex_t* mutex;
pthread_cond_t* cond_var
} SortParams;
static int maximumThreads; /* maximum # of threads to be used */
/* This is an implementation of insert sort, which although it is */
/* n-squared, is faster at sorting short lists than quick sort, */
/* due to its lack of recursive procedure call overhead. */
static void insertSort(char** array, int left, int right) {
int i, j;
for (i = left + 1; i <= right; i++) {
char* pivot = array[i];
j = i - 1;
while (j >= left && (strcmp(array[j],pivot) > 0)) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = pivot;
}
}
/* Recursive quick sort, but with a provision to use */
/* insert sort when the range gets small. */
static void quickSort(void* p) {
SortParams* params = (SortParams*) p;
char** array = params->array;
int left = params->left;
int right = params->right;
int i = left, j = right;
if (j - i > SORT_THRESHOLD) { /* if the sort range is substantial, use quick sort */
int m = (i + j) >> 1; /* pick pivot as median of */
char* temp, *pivot; /* first, last and middle elements */
if (strcmp(array[i],array[m]) > 0) {
temp = array[i]; array[i] = array[m]; array[m] = temp;
}
if (strcmp(array[m],array[j]) > 0) {
temp = array[m]; array[m] = array[j]; array[j] = temp;
if (strcmp(array[i],array[m]) > 0) {
temp = array[i]; array[i] = array[m]; array[m] = temp;
}
}
pivot = array[m];
for (;;) {
while (strcmp(array[i],pivot) < 0) i++; /* move i down to first element greater than or equal to pivot */
while (strcmp(array[j],pivot) > 0) j--; /* move j up to first element less than or equal to pivot */
if (i < j) {
char* temp = array[i]; /* if i and j have not passed each other */
array[i++] = array[j]; /* swap their respective elements and */
array[j--] = temp; /* advance both i and j */
} else if (i == j) {
i++; j--;
} else break; /* if i > j, this partitioning is done */
}
pthread_mutex_lock(params->mutex);
// Wait if the maximum number of threads is reached
while(*params->currentThreads >= maximumThreads) {
pthread_cond_wait(params->cond_var, params->mutex);
}
// Increment the current thread count
(*params->currentThreads)++;
pthread_mutex_unlock(params->mutex);
SortParams first = {params->array, left, j, params->currentThreads, params->mutex, params->cond_var};
SortParams second = {params->array, i, right, params->currentThreads, params->mutex, params->cond_var};
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, (void*)quickSort, &first);
pthread_create(&thread2, NULL, (void*)quickSort, &second);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
// After the threads finish, decrement the current thread count
// and signal any waiting threads
pthread_mutex_lock(params->mutex);
(*params->currentThreads)--;
pthread_cond_signal(params->cond_var);
pthread_mutex_unlock(params->mutex);
} else insertSort(array,i,j); /* for a small range use insert sort */
}
/* user interface routine to set the number of threads sortT is permitted to use */
void setSortThreads(int count) {
maximumThreads = count;
}
/* user callable sort procedure, sorts array of count strings, beginning at address array */
void sortThreaded(char** array, unsigned int count) {
SortParams parameters;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER;
int currentThreads = 0;
parameters.array = array; parameters.left = 0; parameters.right = count - 1;
parameters.currentThreads = ¤tThreads; parameters.mutex = &mutex;
parameters.cond_var = &cond_var;
quickSort(¶meters);
pthread_cond_destroy(&cond_var);
pthread_mutex_destroy(&mutex);
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps
I don't see the logic in here that limits the number of threads. How can we do that?