rpl
rpl

Reputation: 33

Passing pointer as argument with std::thread function C++11

I wanted to pass a pointer to the thread function, but it gives back

error: attempt to use a deleted function __invoke(_VSTD::move(_VSTD::get<0>(__t)), _VSTD::move(_VSTD::get<_In...

Code fragment in main

for (int i = 0; i < threadCount; ++i) {
    ptrTabThreads = new std::thread(checkMin, ptrTab[i]);
    ptrTabThreads->join();
    ++ptrTabThreads;
}

And code for checkMin function

void checkMin(int* tab) {
    int sizeOfTable = 0;

    if (tab == ptrTab[threadCount-1])
        sizeOfTable = partSize + additionalNumbers;
    else
        sizeOfTable = partSize;       

    mt.lock();
    for (int i = 0; i < sizeOfTable; ++i) {
        if (tab[i] < minValue) {
            minValue = tab[i];
        }
    }
    mt.unlock(); 
}

Where ptrTab is an array of pointers:

int* ptrTab[threadCount];

Full code is:

#include <iostream>
#include <thread>
#include <condition_variable>
#include <stdlib.h>
#include <climits>

#define threadCount 10
#define numbersCount 75
std::mutex mt;
int minValue = INT32_MAX;
int partSize, additionalNumbers;
int* ptrTab[threadCount];

void checkMin(int value);
void printTab(int *tab);

int main() {
    int tab[numbersCount];
    srand(time(NULL));

    for (int i = 0; i < numbersCount; ++i) {
        tab[i] = rand() % 1000;
        std::cout << " " << tab[i];
    }

    partSize = numbersCount / threadCount;
    additionalNumbers = numbersCount % threadCount;

    for (int i = 0; i < threadCount-1; ++i) {
        int *newTab = new int[partSize];
        ptrTab[i] = newTab;
    }
    int *newTab = new int[partSize+additionalNumbers];
    ptrTab[threadCount-1] = newTab;

    int copiedElements = 0;
    for (int i = 0; i < threadCount-1; ++i) {
        int *tmpTab = ptrTab[i];
        for (int j = 0; j < partSize; j++) {
            tmpTab[j] = tab[copiedElements];
            copiedElements++;
        }
    }
    int *tmpTab = ptrTab[threadCount-1];
    int elementsLeft = numbersCount-copiedElements;
    for (int i = 0; i < elementsLeft; ++i) {
        tmpTab[i] = tab[copiedElements];
        copiedElements++;
    }

    /*for (int i = 0; i < threadCount; ++i) {
        printTab(ptrTab[i]);
    }*/


    //----------------------

    std::thread tabThreads[threadCount];
    std::thread *ptrTabThreads = tabThreads;

    for (int i = 0; i < threadCount; ++i) {
        ptrTabThreads = new std::thread(checkMin, ptrTab[i]);
        ptrTabThreads->join();
        ++ptrTabThreads;
    }

    std::cout << "\n\n" << minValue << "\n\n";

    //for check
    std::cout << "for check: minimal value is ";
    int min = INT32_MAX;
    for (int i = 0; i < numbersCount; ++i) {
        if (tab[i] < min) {
            min = tab[i];
        }
    }
    std::cout << min << "\n\n";

}

void checkMin(int* tab) {
    int sizeOfTable = 0;

    if (tab == ptrTab[threadCount-1]) 
        sizeOfTable = partSize + additionalNumbers;
    else
        sizeOfTable = partSize;        

    mt.lock();
    for (int i = 0; i < sizeOfTable; ++i) {
        if (tab[i] < minValue) {
            minValue = tab[i];
        }
    }
    mt.unlock();
}

void printTab(int *tab) {
    for (int i = 0; i < 10; ++i) {
        std::cout << tab[i] << " ";
    }
    std::cout << "\n\n";
}

Thank you for all your advices.

Upvotes: 0

Views: 4453

Answers (1)

SergeyA
SergeyA

Reputation: 62563

The immediate problem which triggers compilation error is right here:

void checkMin(int value);

This is the prototype of your function, and it is incorrect - it should be

void checkMin(int* value); //<-- not the pointer.

But this is not the only one! Your code makes no sense. Look at this fragment:

std::thread tabThreads[threadCount];
std::thread *ptrTabThreads = tabThreads;

for (int i = 0; i < threadCount; ++i) {
    ptrTabThreads = new std::thread(checkMin, ptrTab[i]);
    ptrTabThreads->join();
    ++ptrTabThreads;
}

What's the purpose of all this jumping with pointers? You also have a leak in your code, since you are modifying the pointer you obtained from new before deleteing it. Why not use following simple code?

std::array<std::thread, threadCount> tabThreads;

for (int i = 0; i < threadCount; ++i) {
    tabThreads[i] = std::thread(checkMin, ptrTab[i]);
    tabThreads[i].join();
}

This still serves no pratical purpose (application remains effectively single-threaded, since you join your thread right after creating it), but at least, the code is correct. To really do some fancy multithreading, you need your loop to look like following:

for (int i = 0; i < threadCount; ++i)
    tabThreads[i] = std::thread(checkMin, ptrTab[i]);

for (std::thread& t : tabThreads) // so-called range-for loop. Nice thing!
    t.join();

This will paralellize stuff!

Upvotes: 2

Related Questions