Search code examples
multithreadingclassreferencec++-climergesort

Multithreading in C++ using reference classes - ThreadStart constructor issues?


I appreciate any help, and would like to thank you in advance. I'm working on a project for one of my classes. Essentially performing merge sort using multithreading and reference classes. In main I'm just trying to create an initial thread that will begin the recursive mergesort. Each time the array is split a new thread is spawned to handle that subroutine. I don't need all of it done, i just don't under stand why my Thread constructor and ThreadStart delegate are not working. Thanks again!!

#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include <cstdlib>

using namespace System;
using namespace System::Threading;

public ref class MergeSort 
{
    private: int cnt;

    public: MergeSort() 
    {
        cnt = 0;
    }

    public: void mergeSort(char a[], int from, int to)
    {
        Thread^ current = Thread::CurrentThread;

        if(from == to)
            return;
        int mid = (from + to)/2;

        //Sort the first and the second half
        //addThread(a, from, mid);
        //addThread(a, mid+1, to);

        //threads[0]->Join();
        //threads[1]->Join();

        merge(a, from, mid, to);
    }

    public: void merge(char a[], int from, int mid, int to)
    {           
            Thread^ current = Thread::CurrentThread;
            while (current ->ThreadState == ThreadState::Running)
            {
                    int n = to-from + 1; // Size of range to be merged
                    std::vector<char> b(n);

                    int i1 = from; //Next element to consider in the first half
                    int i2 = mid + 1; //Next element to consider in the second half
                    int j = 0; //Next open position in b

                    //As long as neight i1 or i2 is past the end, move the smaller element into b
                    while(i1 <= mid && i2 <= to)
                    {
                        if(a[i1] < a[i2])
                        {
                            b[j] = a[i1];
                            i1++;
                        }
                        else
                        {
                            b[j] = a[i2];
                            i2++;
                        }
                        j++;
                    }

                    //Copy any remaining entries of the first half
                    while(i1 <= mid)
                    {
                        b[j] = a[i1];
                        i1++;
                        j++;
                    }
                    while(i2 <= to)
                    {
                        b[j] = a[i2];
                        i2++;
                        j++;
                    }

                    //Copy back from temporary vector
                    for(j = 0; j < n; j++)
                        a[from+j] = b[j];
            }       
    }
};


void main()
{
    char A[10];

    for(int i = 0; i < 10; i++)
    {
        A[i] = ((char) ((rand() % (122-65)) + 65));
    }

    array<Thread^>^ tr = gcnew array<Thread^>(10);

    MergeSort^ ms1 = gcnew MergeSort();

    ThreadStart^ TS = gcnew ThreadStart(ms1, &MergeSort::mergeSort(A, 0, 10));
    tr[0] = gcnew Thread(TS);
    tr[0] -> Start();

    system("pause");
}

Solution

  • The issue you are facing here is how to construct a ThreadStart delegate. You are trying to do too many things in the ThreadStart constructor. You cannot pass in arguments at this point because all it is looking for is a start location for the thread.

    The delegate should be:

    ThreadStart^ TS = gcnew ThreadStart(ms1, &MergeSort::mergeSort);
    

    Since however you are passing in some state, I would recommend doing a bit more research on how that is done using C++\CLI. This MSDN topic should give you a start.