创建新线程时编译器错误:“调用:找不到匹配的重载函数”

问题描述 投票:0回答:1

我有以下 C++ 代码:

#include <iostream>
#include <thread>
#include <mutex>
#include <vector>
#include <chrono>

void ThreadFunc(std::vector<int>* pIntVector, std::mutex* pMtx, int i, int j) {
    std::lock_guard<std::mutex> lock(*pMtx);
    for ( i; i < j; i++)
    {
        pIntVector->push_back(i);
        std::wcout << L"Current thread: " << std::this_thread::get_id() << L"\n";
        std::this_thread::sleep_for(std::chrono::milliseconds(500));
    }
}

int main() {
    const std::vector<int> vec{};
    const std::mutex mtx{};

    std::thread t1(ThreadFunc, &vec, &mtx, 0, 50);
    std::thread t2(ThreadFunc, &vec, &mtx, 100, 150);

    t1.join();
    t2.join();

    return 0;
}

当我尝试使用 MSVC++ 编译此文件时,会引发以下错误:

C:/data/msvc/14.36.32530-Pre/include\thread(55): error C2672: 'std::invoke': no matching overloaded function found
C:/data/msvc/14.36.32530-Pre/include\type_traits(1580): note: could be 'unknown-type std::invoke(_Callable &&,_Ty1 &&,_Types2 &&...) noexcept(<expr>)'
C:/data/msvc/14.36.32530-Pre/include\thread(55): note: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Ty1 &&,_Types2 &&...) noexcept(<expr>)'
C:/data/msvc/14.36.32530-Pre/include\thread(55): note: With the following template arguments:
C:/data/msvc/14.36.32530-Pre/include\thread(55): note: '_Callable=void (__cdecl *)(std::vector<int,std::allocator<int>> *,std::mutex *,int,int)'
C:/data/msvc/14.36.32530-Pre/include\thread(55): note: '_Ty1=const std::vector<int,std::allocator<int>> *'
C:/data/msvc/14.36.32530-Pre/include\thread(55): note: '_Types2={const std::mutex *, int, int}'
C:/data/msvc/14.36.32530-Pre/include\type_traits(1574): note: or       'unknown-type std::invoke(_Callable &&) noexcept(<expr>)'
C:/data/msvc/14.36.32530-Pre/include\thread(55): note: 'unknown-type std::invoke(_Callable &&) noexcept(<expr>)': expects 1 arguments - 5 provided
C:/data/msvc/14.36.32530-Pre/include\thread(62): note: see reference to function template instantiation 'unsigned int std::thread::_Invoke<_Tuple,0,1,2,3,4>(void *) noexcept' being compiled
        with
        [
            _Tuple=_Tuple
        ]
C:/data/msvc/14.36.32530-Pre/include\thread(71): note: see reference to function template instantiation 'unsigned int (__cdecl *std::thread::_Get_invoke<_Tuple,0,1,2,3,4>(std::integer_sequence<size_t,0,1,2,3,4>) noexcept)(void *)' being compiled
C:/data/msvc/14.36.32530-Pre/include\thread(88): note: see reference to function template instantiation 'void std::thread::_Start<void(__cdecl &)(std::vector<int,std::allocator<int>> *,std::mutex *,int,int),_Ty,const std::mutex*,int,int>(_Fn,_Ty &&,const std::mutex *&&,int &&,int &&)' being compiled
        with
        [
            _Ty=const std::vector<int,std::allocator<int>> *,
            _Fn=void (__cdecl &)(std::vector<int,std::allocator<int>> *,std::mutex *,int,int)
        ]
<source>(21): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl &)(std::vector<int,std::allocator<int>> *,std::mutex *,int,int),const std::vector<int,std::allocator<int>>*,const std::mutex*,int,int,0>(_Fn,const std::vector<int,std::allocator<int>> *&&,const std::mutex *&&,int &&,int &&)' being compiled
        with
        [
            _Fn=void (__cdecl &)(std::vector<int,std::allocator<int>> *,std::mutex *,int,int)
        ]

导致此错误的原因是什么以及如何解决该问题?

编辑:我将

vec
mtx
都设为非常量,现在程序按预期工作。但我不明白的是,将变量声明为
const
本质上不是使其成为对象的“常量引用”吗?这使我们能够修改其成员?

顺便说一句,我有 C# 背景。

c++ stdthread
1个回答
0
投票

提供的代码存在多个问题:

  1. 创建
    const
    mutex
    vector
    然后修改它们 无法工作
  2. 当使用引用而不是原始指针时,您需要使用std::ref将对象传递到线程构造函数中
  3. 多线程应该没有积极的影响,因为互斥体会立即锁定其他线程,并且只有在完全完成计算后才释放

以下代码编译使用

void ThreadFunc(std::vector<int> &pIntVector, std::mutex &pMtx, int i, int j) 
{
    // ... change `->` to `.`
}

int main() {
  std::vector<int> vec{};
  std::mutex mtx{};

  std::thread t1(ThreadFunc, std::ref(vec), std::ref(mtx), 0, 50);
  std::thread t2(ThreadFunc, std::ref(vec), std::ref(mtx), 100, 150);

  //...
}
© www.soinside.com 2019 - 2024. All rights reserved.