2015-06-26 86 views
4

問題陳述 - TL;博士
將號碼添加到載體中,然後將它們輸出傳遞的功能和參數線程

詳細

我的目的是使一個類Foo,其中包含我可以以線程安全方式填充的std::vector<int>。我創建了一個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; 
}; 

我再發,我可以用它來創建一個線程一個免費的功能,沒有Foo的內部的任何知識。

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

我的目的是以下值添加到向量(不論以便他們結束了由於線時間)

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

下面是一個簡單的測試,看看是否能在工作。

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(); 
} 

問題
上面的代碼won't compile

這是錯誤消息

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...>) 
     ^

我不太明白,錯誤消息。我是不是正確地將線程添加到矢量threads

+4

您可能必須將'foo'封裝在'std :: ref'中。線程不允許事物在沒有包裝的情況下通過引用傳遞。儘管這是一個晦澀的錯誤信息。 – Carcigenicate

+0

@Carcigenicate你完全正確,修復它! – CoryKramer

+1

好。在黑暗中獲得愛情照片。 – Carcigenicate

回答

7

您必須將foo包裝在std::ref中。線程不允許事物在沒有包裝的情況下通過引用傳遞。