Trying to create mergeSort multithreaded using c

I'm trying to create a function that will create threads and use them for mergeSort.
So far i have this:
this is for combining the sorted arrays.

void merge(int *arr, int min,int mid,int max)
{
    int *tmp = malloc((max-min+1)*sizeof(int));
    int i,j,k,m;
    j=min;
    m=mid+1;
    for(i=min; j<=mid && m<=max ; i++)
    {
        if(arr[j]<=arr[m])
        {
            tmp[i-min]=arr[j];
            j++;
        }
        else
        {
            tmp[i-min]=arr[m];
            m++;
        }
    }
    if(j>mid)
    {
        for(k=m; k<=max; k++)
        {
            tmp[i-min]=arr[k];
            i++;
        }
    }
    else
    {
        for(k=j; k<=mid; k++)
        {
            tmp[i-min]=arr[k];
            i++;
        }
    }
    for(k=min; k<=max; k++)
        arr[k]=tmp[k-min];

    free(tmp);
}

void mergeSort(int *array, int min, int max, void(*submergeSort)(int *, int, int, int, int) ){
    int mid;
    if(min < max){
        mid=(min+max)/2;
        
        submergeSort(array, min, mid, mid+1, max);

        merge(array, min, mid, max);
    }
}

and this is my function for creating threads:

void submergeSortThread(int* array, int min1, int max1, int min2, int max2){
       pthread_t t1,t2;
       pthread_create(&t1, NULL, mergeSort,(void*)array,min1,max1);  
       pthread_create(&t2, NULL, mergeSort,(void*)array,min2,max2);  
       
      }

and i'm stuck here. I don't know if it's even the correct way to create threads like this?

Thank u for the help

With what, exactly?

If you have an error someone might be able to help solve it. Otherwise, if it's just a general code review this probably isn't a great place - StackOverflow would probably be better.

No if he asks this way, he'll only get downvotes and eventually his question will be closed. StackOverflow community hates this kind of statement:

which shows inadequate effort, as pthreads is well documented. How could you use a function you don't know what it expects and the corresponding behavior given the parameters?

3 Likes

On another point - it looks like homework :slight_smile:.

https://wiki.manjaro.org/index.php?title=Forum_Rules#Homework

1 Like

As far as I see it is all there, you just need to call the mergeSort function.

Forum kindly sponsored by