-1

I've been trying to work with threads and have an issue I cant seem to fix. I'm trying to spawn a thread from a member function that takes parameters however I have yet to be able to get it to not throw errors.

Heres what I have

ArraySorter s;
char *arr1, *arr2, *arr3;

arr1 = new char[MAXSIZE];
arr2 = new char[MAXSIZE];
arr3 = new char[MAXSIZE];

srand(time(NULL));

for (int i = 0; i < MAXSIZE; i++)
{
    arr1[i] = arr2[i] = arr3[i] = rand() % MAXSIZE;
}

thread t1(&ArraySorter::InsertionSort, &s, arr1, MAXSIZE);
thread t2(&ArraySorter.MergeSort, arr2, MAXSIZE);
thread t3(&s.QuickSort, arr3, MAXSIZE);

To be clear all my sorting algorithms take a char * and integer in that order, All 3 ways of spawning the threads above do not work and I'm at a loss since I've tried the solutions I've found.

Declarations:

static void InsertionSort(int* arr, int n, int startIndex, int gap);
static void InsertionSort(int* arr, int n);
static void MergeSort(int* arr, int n);
static void QuickSort(int* arr, int n);
static void ShellSort(int* arr, int n, int* gapVals, int gapValsCount);
static void QuickSort(int *arr, int high, int low);
static int GetPivot(int *arr, int high, int low);
static void MergeArray(int *arr, int high, int mid, int low);
static void MergeSort(int *arr, int high, int low);
  • Thanks for the update. The first one will compile if you remove the `&s` parameter. These are static members, no object is required. The rest will follow that same form. You shouldn't even need `s` declared. – WhozCraig Apr 30 '15 at 06:28

1 Answers1

0
  1. You create array of char and try to pass char*, but functions take int*. Either you need to change type somewhere, or make sorting functions templates.

  2. You're not able to compile code because thread constructor could not deduce what function to use (for example void InsertionSort(int* arr, int n, int startIndex, int gap) or void InsertionSort(int* arr, int n)). One way to correct it is to rename functions. Another is to specify function pointer like in this answer.

    void(*insertion_1)(int*, int)      = &ArraySorter::InsertionSort;
    void(*insertion_2)(int*, int, int) = &ArraySorter::InsertionSort;
    

See full code of simplified example

#include <iostream>
#include <functional>
#include <thread>

using namespace std;

#define MAXSIZE 128

class ArraySorter {
public:
    static void InsertionSort(int* arr, int n) {
    }
    static void InsertionSort(int* arr, int n1, int n2) {
    }
};

int main() {
    int* arr1 = new int[MAXSIZE];
    int* arr2 = new int[MAXSIZE];
    int* arr3 = new int[MAXSIZE];

    for (int i = 0; i < MAXSIZE; i++) {
        arr1[i] = arr2[i] = arr3[i] = rand() % MAXSIZE;
    }

    void(*insertion_1)(int*, int)      = &ArraySorter::InsertionSort;
    void(*insertion_2)(int*, int, int) = &ArraySorter::InsertionSort;
    thread t1(insertion_1, arr1, MAXSIZE);
    thread t2(insertion_2, arr1, MAXSIZE, 0);
    // Won't compile!
    // thread t3(&ArraySorter::InsertionSort, arr1, MAXSIZE);
}
Community
  • 1
  • 1
Nikolay K
  • 3,770
  • 3
  • 25
  • 37