std::thread Args... 列表中的函数指针

Function pointer in a std::thread Args... list

我正在尝试提供指向我的 std::thread 参数列表的函数指针,但我遇到了一堆我不理解的编译错误 (@ MSVC.28.29333\include\thread(43,14): error C2672: 'invoke' : fonction correspondante surchargée introuvable [overloaded function not found])。

我可以写一个给出同样错误的 mcve。

#include <thread>
#include <vector>

template<typename T>
void worker(std::vector<T>& data_set, void(*do_something)(T&)) {
    for (T& t : data_set)
        (*do_something)(t);
}

template<typename T>
std::vector<T> get_data(void(*do_something)(T&), size_t sz) {

    //only 1 thread as example
    std::vector<T> data_set(sz);
    std::thread t1(worker<T>, data_set, do_something); //compile error
    t1.join();


    worker<T>(data_set, do_something); //this on the other hand does compile

    return data_set;
}

void do_something_int(int& i) {
    i = 1;
}

void do_something_float(float& f) {
    f = 2.1f;
}

void do_something_char(char& c) {
    c = 'a';
}

int main(int argc, char** argv) {

    auto data_set_int = get_data(&do_something_int, 100);
    auto data_set_float = get_data(&do_something_float, 100);
    auto data_set_char = get_data(&do_something_char, 100);

    return 0;
}

有趣的是,如果我以非线程方式调用 worker,一切都很好。我不知道编译器期望什么。

问题在于您的函数通过非常量左值引用接受参数。 std::thread 会将右值传递给函数,非常量左值引用不能绑定到右值。

为了传递左值,您必须使用引用包装器:

std::thread t1(worker<T>, std::ref(data_set), do_something);

在单独的线程中引用自动对象时,请始终注意确保引用对象的生命周期。