简体   繁体   English

将函数和参数传递给线程

[英]Passing a function and arguments to a thread

Problem Statement - tl;dr 问题陈述 - tl;博士
Add numbers to a vector, then output them 将数字添加到矢量,然后输出它们

Details 细节

My intention was to make a class Foo , that contained a std::vector<int> that I could populate in a thread-safe manner. 我的目的是创建一个类Foo ,它包含一个std::vector<int> ,我可以用线程安全的方式填充它。 I created an AddValue method to allow adding values to that vector keeping thread-safety in mind. 我创建了一个AddValue方法,允许向该向量添加值,同时牢记线程安全性。

std::mutex mt;
class Foo
{
public:
    void AddValue(int i)
    {
        std::lock_guard<std::mutex> lg{ mt };
        values.push_back(i);
    }

    void PrintValues() const
    {
        for (int i : values)
        {
            std::cout << i << " ";
        }
    }

private:
    std::vector<int> values;
};

I then made a free function that I could use to create a thread, without any knowledge of the internals of Foo . 然后我创建了一个自由函数,我可以用来创建一个线程,而不知道Foo的内部。

void Func(Foo& foo, int t)
{    
    for (int i = 0; i < t; i++)
    {
        foo.AddValue(i);
    }
}

My intention was to add the following values to the vector (in whatever order they ended up due to the thread timing) 我的目的是将以下值添加到向量中(由于线程时序,它们以任何顺序结束)

{3, 2, 2, 1, 1, 1, 0, 0, 0, 0}

Here is a quick test to see if that was working. 这是一个快速测试,看看它是否有效。

int main()
{
    Foo foo;
    std::vector<std::thread> threads;
    for (int i = 1; i < 5; ++i)
    {
        threads.emplace_back(Func, foo, i);
    }

    std::for_each(begin(threads), end(threads), [](std::thread& t){ t.join(); });

    foo.PrintValues();
}

Problem 问题
The above code won't compile 上面的代码不会编译

This is the error message 这是错误消息

In file included from /usr/include/c++/4.9/mutex:42:0,
                 from 3:
/usr/include/c++/4.9/functional: In instantiation of 'struct std::_Bind_simple<void (*(Foo, int))(Foo&, int)>':
/usr/include/c++/4.9/thread:140:47:   required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(Foo&, int); _Args = {Foo&, int&}]'
/usr/include/c++/4.9/ext/new_allocator.h:120:4:   required from 'void __gnu_cxx::new_allocator< <template-parameter-1-1> >::construct(_Up*, _Args&& ...) [with _Up = std::thread; _Args = {void (&)(Foo&, int), Foo&, int&}; _Tp = std::thread]'
/usr/include/c++/4.9/bits/alloc_traits.h:253:4:   required from 'static std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::thread; _Args = {void (&)(Foo&, int), Foo&, int&}; _Alloc = std::allocator<std::thread>; std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> = void]'
/usr/include/c++/4.9/bits/alloc_traits.h:399:57:   required from 'static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::thread; _Args = {void (&)(Foo&, int), Foo&, int&}; _Alloc = std::allocator<std::thread>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]'
/usr/include/c++/4.9/bits/vector.tcc:97:40:   required from 'void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {void (&)(Foo&, int), Foo&, int&}; _Tp = std::thread; _Alloc = std::allocator<std::thread>]'
44:42:   required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named 'type' in 'class std::result_of<void (*(Foo, int))(Foo&, int)>'
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.9/functional:1695:9: error: no type named 'type' in 'class std::result_of<void (*(Foo, int))(Foo&, int)>'
         _M_invoke(_Index_tuple<_Indices...>)
         ^

I don't quite understand that error message. 我不太明白这个错误信息。 Am I not adding the threads to the vector threads correctly? 我没有正确地将线程添加到矢量threads吗?

You have to wrap the foo in a std::ref . 你必须将foo包装在std::ref Threads don't allow things to be passed by reference without that wrapper. 如果没有该包装器,线程不允许通过引用传递内容。

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

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