2014-07-26 15 views
5

Ho una funzione che accetta un vettore di stringhe e ho una serie di quei vettori di stringhe in un vettore.Provare a passare il contenuto di un vettore a un thread

Volevo passare in rassegna il vettore che passa ogni vettore di stringhe a un nuovo thread.

for (vector<vector<string> >::iterator it = vecstringvec.begin() ; 
    it != vecstringvec.end(); ++it){ 
    threadvector.push_back(thread(func, *it)); 
} 

In sostanza, eccetto quanto sopra non funziona (errore di compilazione). Quello che credo è che ho bisogno di un std :: ref del vettore per passare, ma non sono sicuro esattamente, o come farlo usando l'iteratore.

errore completo:

/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::vector<std::basic_string<char> >, std::reference_wrapper<std::vector<std::basic_string<char> > >))(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >&)>’: 
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >&); _Args = {std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::reference_wrapper<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >}]’ 
prog.cpp:199:55: required from here 
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<std::basic_string<char> >, std::reference_wrapper<std::vector<std::basic_string<char> > >))(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >&)>’ 
     typedef typename result_of<_Callable(_Args...)>::type result_type; 
                  ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::vector<std::basic_string<char> >, std::reference_wrapper<std::vector<std::basic_string<char> > >))(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >&)>’ 
     _M_invoke(_Index_tuple<_Indices...>) 
     ^
make: *** [prog.o] Error 1 
+0

Cosa ti fa pensare che non funzioni? – Johan

+0

Quando ci si è seduti "non funziona", cosa intendi con questo? Puoi per favore elaborare? –

+0

Un errore apparentemente troppo lungo per la casella di commento. Un sacco di cose come questa: ^ /usr/include/c++/4.8/functional:1727:9: errore: nessun tipo chiamato 'tipo' in 'classe std :: result_of )) (std :: basic_string , std :: vector >> &)>' _M_invoke (_Index_tuple <_Indices...>) – user3816764

risposta

7

Guardando il messaggio di errore, func() vettore dei parametri è un riferimento pass-by-e non un valore passaggio per. Prova a utilizzare std::ref(*it).

Citando cppreference:

The arguments to the thread function are copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g. with std::ref or std::cref).

Esempio:

#include <vector> 
#include <thread> 
#include <iostream> 

void func(const std::vector<int>& value) 
{ 
    std::cout << "Value: " << value.size() << std::endl; 
} 

int main(int argc, char const *argv[]) 
{ 
    std::vector<std::vector<int>> values = 
     { { 1, 2 } , { 1, 2, 3 }, { 1, 2, 3, 4 } }; 

    std::vector<std::thread> threads; 
    for (auto& vect: values) 
    { 
     threads.emplace_back(func, std::cref(vect)); 
    } 

    for (auto& thread: threads) 
    { 
     thread.join(); 
    } 

    return 0; 
} 

(BTW la diagnostica sarebbe stato molto più facile con un auto piena contenuto esempio).

+0

Ho provato a usare std :: ref (* it) senza alcun risultato. – user3816764

+0

Sembra che l'OP voglia passare un riferimento non const a 'func' - che penso funzionerà solo con il tuo codice. –

+1

@ user3816764 Il mio esempio è compilato e funziona. Puoi individuare alcune differenze nel tuo codice? – Johan

Problemi correlati