简体   繁体   English

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

[英]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... 错误:尝试使用已删除的函数__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 以及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(); 
}

Where ptrTab is an array of pointers: 其中ptrTab是指针数组:

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. 感谢您的所有建议。

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 delete ing it. 您的代码也有泄漏,因为您正在修改从new获得的指针,然后再delete它。 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! 这将使东西并列!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM