0

I'm relatively new to programming so pardon the messy code. I am trying to get into multithreading with different sorting methods and am starting out with a simple bubblesort. I am getting an error with the pthread_create function call. I have tried several slight variations to my syntax, but I am not really sure what my mistake is. Any help would be much appreciated. (Also any tips on the rest of my code if you notice something.)

~~Only relevant code included

#include <iostream>
#include <pthread.h>
#include <cmath>
using namespace std;

//Bubblesort Method
void * bubbleSort(int arr[], int size) {
    int i, j = 1;
    while (j != 0) {
        j = 0;
        i = 0;
        while (i < size) {
            if (arr[i] > arr[i+1]) {
                swap(arr[i], arr[i+1]); //Swap function defined elsewhere in code
                j++;
            }
            i++;
        }
    }
}

int main {

//Define pthreads
    pthread_t tid1, tid2, tid3, tid4;
    pthread_t *pthreads[] = {&tid1, &tid2, &tid3, &tid4};

//Fill smaller arrays
    int smallArraySize = div / 4;
    int *first = (int *) malloc(smallArraySize * sizeof(int));
    int *second = (int *) malloc(smallArraySize * sizeof(int));
    int *third = (int *) malloc(smallArraySize * sizeof(int));
    int *fourth = (int *) malloc(smallArraySize * sizeof(int));

    for (int k = 0; k < smallArraySize; k++){
        first[k] = arr[k];
    }
    for (int k = 0; k < smallArraySize; k++){
        second[k] = arr[k+smallArraySize];
    }
    for (int k = 0; k < smallArraySize; k++){
        third[k] = arr[k+2*smallArraySize];
    }
    for (int k = 0; k < smallArraySize; k++){
        fourth[k] = arr[k+3*smallArraySize];
    }

    int smallArray[] = {*first, *second, *third, *fourth};

    for (int j = 0; j < div; j++) {
        pthread_create(pthreads[j], NULL,* bubbleSort, (smallArray[j],smallArraySize));
    }
    return 0;
}

  • 1
    The main function can be spelled `int main()`, but not `int main`. `int main {` does not define a function. – Drew Dormann Jun 24 '22 at 17:19
  • 3
    "_I'm relatively new to programming so pardon the messy code._": Don't use pthreads in new C++ code. Use `std::thread` instead. Don't use raw arrays in C++. Use `std::vector` (or `std::array`) instead. Don't use `malloc` in C++, use `new` instead (but really use `std::vector`). Your code looks like C, not C++. The only C++ thing in there are the `#include`s and `using namespace std;` and `swap`. Don't write C if you want to learn C++. [Book recommendations](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user17732522 Jun 24 '22 at 17:21
  • 2
    Your bubbleSort() function does not match the signature that pthread_create() is expecting for a thread function. Read the [documentation](https://man7.org/linux/man-pages/man3/pthread_create.3.html). You can't just create a thread out of any random function. There are certain requirements that must be met. Also, where is `div` defined, and why are you *dividing* it by 4? – Remy Lebeau Jun 24 '22 at 17:33
  • Looks like someone has been teaching C, not C++. – user4581301 Jun 24 '22 at 17:33
  • Side note: When you don't know what the problem is, it can be hard to determine exactly what is or is not relevant. The best defense against this is to make a [mre]. Odds are good that making one will allow you to see and fix the error yourself, so if you make one early in the question-asking process odds are good you'll be done faster. – user4581301 Jun 24 '22 at 17:36
  • 3
    Also, it probably is not a good idea to start trying to write multi-threaded code when you are only just beginning to learn the language. – Solomon Slow Jun 24 '22 at 18:04

0 Answers0