So I have a quick sort algorithm that I want to run in two different threads, the idea is to have 2 independent halves of the array being sorted at once (this should not be a problem by quick sort nature of partition).
My code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void troca (int *v, int i, int j);
int partition (int *v, int ini, int fim);
void quicksort (int *v, int ini, int fim, int size);
typedef struct {
int ini, mid, fim;
} thread_arg;
int size;
int *v;
void *thread_func(void* arg){
thread_arg *a = arg;
quicksort(v, a->ini, a->mid, a->fim);
printf("done in\n");
return NULL;
}
int main()
{
// initializations
scanf("%d", &size);
v = malloc(size * sizeof(int));
// read array
for (int i = 0; i < size; ++i)
scanf("%d", &v[i]);
// arguments
thread_arg argument1, argument2;
int mid = partition(v, 0, size-1);
argument1.ini = 0;
argument1.mid = mid-1;
argument1.fim = size;
argument2.ini = mid;
argument2.mid = size-1;
argument2.fim = size;
pthread_t thread1, thread2;
// thread and execution
pthread_create(&thread1, NULL, thread_func, &argument1);
printf("done out 1\n");
pthread_create(&thread2, NULL, thread_func, &argument2);
printf("done out 2\n");
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
free(v);
return 0;
}
void quicksort (int *v, int ini, int fim, int size){
if (ini >= fim)
return;
int meio = partition(v, ini, fim);
quicksort(v, ini, meio-1, size);
quicksort(v, meio+1, fim, size);
}
int partition (int *v, int ini, int fim){
int i = ini-1, j = ini;
troca(v, (ini+fim)/2, fim);
int pivo = v[fim];
for (; j < fim; ++j)
{
if (v[j] <= pivo)
{
i++;
troca(v, i, j);
}
}
troca(v, i+1, fim);
return i+1; //indice do pivo;
}
void troca (int *v, int i, int j){
int aux = v[i];
v[i] = v[j];
v[j] = aux;
return;
}
The execution works and sorts perfectly, it does generates 2 new independent threads that sorts halves of the array each. The problem is, it does not do it at the same time. Running the program with a input of 100m random numbers:
done out 1
done out 2
done in
done in
real 0m47,464s
user 0m50,686s
sys 0m0,452s
But it takes about 25 seconds for the first 3 lines to appear, and ~25 for the last one, which indicates that the second thread is waiting for the first one to run.
In the htop console, it appears that in some point both run at the same time (this is backed at the fact that this program runs a bit faster than my normal one)
Finally, I understand that it is not safe to work simultaneously with this sort of data, but on this sorting example it should be fine.
argument1.fim = size;Should that besize / 2?htop