C ++多线程程序尝试使用已删除的功能

时间:2019-01-11 06:30:27

标签: c++ multithreading

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

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

在此演示中,我使用“ 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);

2 个答案:

答案 0 :(得分:4)

问题是msg.body = '''Dear admin, You requested that your password be reset. Please visit the link below or copy and paste it into your browser to create a new password.%s''' \ %url_for('.get_resetpassword',token=token,_external=True)+''' Thank you, Team ''' 对象需要能够复制传递给线程的参数。并且引用无法复制。

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

std::thread

答案 1 :(得分:0)

如果您尝试使用引用,则使用线程是令人惊讶的。另一个答案是完全正确的,请使用std::ref传递参数。

我个人更喜欢使用lambda:

std::thread thread_read([&data](){ read_value(data); });
std::thread thread_print([&data = std::as_const(data)](){ print_value(data); });

它可能不如std::ref优雅,尽管它使用了您在所有其他地方都已经知道的概念,而不是要求您记住意外复制。