使用 std::thread 函数 C++11 传递指针作为参数

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

我想传递一个指向线程函数的指针,但它返回

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

main中的代码片段

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

以及 checkMin 函数的代码

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(); 
}

其中 ptrTab 是一个指针数组:

int* ptrTab[threadCount];

完整代码为:

#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";
}

感谢您的所有建议。

直接导致编译错误的问题就在这里:

void checkMin(int value);

这是你的函数原型,不正确 - 应该是

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

但这不是唯一的!你的代码没有意义。看这个片段:

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;
}

所有这些用指针跳跃的目的是什么?您的代码也有泄漏,因为您正在修改从 new 获得的指针,然后再 deleteing 它。为什么不使用下面的简单代码?

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

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

这仍然没有任何实际用途(应用程序实际上仍然是单线程的,因为您在创建线程后立即加入线程),但至少,代码是正确的。要真正做一些花哨的多线程,你需要你的循环如下所示:

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();

这将使东西并行化!