C++多线程程序试图使用删除的函数

标签 c++ multithreading

我是C++多线程程序领域的新手。我想从我的磁盘中读取 block 数据并根据这些数据进行一些计算。为了简化代码,我写了下面的demo来测试我的想法。但是,我发现了一些问题。

#include <iostream>
#include <thread>
#include <string>
#include <mutex>
#include <vector>
#include <condition_variable>

std::condition_variable cv;
std::mutex cv_m;

bool is_print;

void read_value(std::vector<int> &data)
{
    for (int j = 1; j < 5; j++)
    {
        std::cout << "read data iteration is " << j << std::endl;
        std::lock_guard<std::mutex> lk(cv_m);
        for (int i = 0; i < 5; i++)
            data.at(i) = i * j;
        is_print = true;
        cv.notify_one();
    }
}

void print_value(const std::vector<int> &data)
{
    std::cout << "output data" << std::endl;
    std::unique_lock<std::mutex> lk(cv_m);
    cv.wait(lk, []{return is_print;});
    for (int i = 0; i < data.size(); i++)
        std::cout << data[i] << std::endl;
    is_print = false;
}

int main()
{
    is_print = false;
    std::vector<int> data = {0, 0, 0, 0, 0};

    std::thread thread_read(read_value, data);
    std::thread thread_print(print_value, data);

    thread_print.join();
    thread_read.join();

    return 0;
}

在这个demo中,我使用“read_value”来模拟从磁盘读取数据,我想多次从磁盘读取数据。因此,我在函数“read_value”中添加了一个外循环。之后,我使用“print_value”函数输出数据。但是,在这种情况下,我会遇到如下错误,这告诉我我使用了已删除的功能。我不确定问题是什么以及如何多次读取数据。非常感谢!

In file included from 

/Users/zsk/Downloads/programming/C++/multi_thread/multi_thread/main.cpp:10:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:342:5: error: attempt to use a deleted function
    __invoke(_VSTD::move(_VSTD::get<1>(__t)), _VSTD::move(_VSTD::get<_Indices>(__t))...);
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:352:5: note: in instantiation of function template specialization 'std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void (*)(std::__1::vector<int, std::__1::allocator<int> > &), std::__1::vector<int, std::__1::allocator<int> > , 2>' requested here
    __thread_execute(*__p, _Index());
    ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/thread:368:47: note: in instantiation of function template specialization 'std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void (*)(std::__1::vector<int, std::__1::allocator<int> > &), std::__1::vector<int, std::__1::allocator<int> > > >' requested here
    int __ec = __libcpp_thread_create(&__t_, &__thread_proxy<_Gp>, __p.get());
                                              ^
/Users/zsk/Downloads/programming/C++/multi_thread/multi_thread/main.cpp:51:17: note: in instantiation of function template specialization 'std::__1::thread::thread<void (&)(std::__1::vector<int, std::__1::allocator<int> > &), std::__1::vector<int, std::__1::allocator<int> > &, void>' requested here
    std::thread thread_read(read_value, data);

最佳答案

问题是 std::thread 对象需要能够复制您传递给线程的参数。并且不能复制引用。

要解决此问题,您需要使用 std::ref or std::cref包装参数:

std::thread thread_read(read_value, std::ref(data));
std::thread thread_print(print_value, std::cref(data));

关于C++多线程程序试图使用删除的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54141425/

相关文章:

c++ - 在多线程环境中将局部变量作为常量引用传递

c# - 如何在最后一个任务完成后停止进度条?

c - Linux线程优先级

c# - 如何将.exe从C++项目复制到C#项目的输出目录?

c++ - result_of 对我不起作用

C++返回引用/堆栈内存

c++ - shared_ptr 交换线程安全吗?

python - 每个连接都有单独的线程?

c++ - MATLAB 代码与 C/C++ 集成时出现 fatal error

c++ - OpenGL 与 OpenCV 相结合的计算机视觉教程