2012-10-31 3 views
11

Questo è un seguito di una domanda precedente (here), ma sto lavorando a un'applicazione multithread e vorrei pubblicare un pacchetto Boost packaged_task su un io_service. Sono bloccato usando un compilatore C++ 03 (quindi std :: move è fuori), e packaged_task non è copiabile. Ho provato a metterlo in un shared_ptr e a passarlo, e molte altre cose. Ecco il mio attuale tentativo e gli errori del compilatore successivi. Qualche idea su come farlo funzionare?Come si pubblica un boost packaged_task su un io_service in C++ 03?

boost::asio::io_service io_service; 
boost::thread_group threads; 
boost::asio::io_service::work work(io_service); 
for (int i = 0; i < maxNumThreads; ++i) 
{ 
    threads.create_thread(boost::bind(&boost::asio::io_service::run, 
     &io_service)); 
} 
std::vector<boost::shared_future<bool> > pending_data; // vector of futures 

bool process_data(int,int){...} 
... 

for(int theTime = 0; theTime != totalScenarioTime; ++theTime) 
{ 
    for(int i = 0; i < numSmallTasks; ++i) 
    { 
     boost::packaged_task<bool> task(boost::bind(&process_data,i,theTime)); 
     boost::shared_future<bool> fut(task.get_future()); 
     pending_data.push_back(fut); // C++11 possible: (std::move(fut) when fut is a unique_future); 
     io_service.post(task); // C++11 possible: (std::move(task));  
    } 
    // After loop - wait until all futures are evaluated 
    boost::wait_for_all(pending_data.begin(), pending_data.end()); 
    pending_data.clear(); 
} 

Questo si traduce in:

In file included from ../boostlibs/boost/asio/io_service.hpp:767:0, 
      from ../boostlibs/boost/asio/basic_io_object.hpp:19, 
      from ../boostlibs/boost/asio/basic_socket.hpp:19, 
      from ../boostlibs/boost/asio/basic_datagram_socket.hpp:20, 
      from ../boostlibs/boost/asio.hpp:20, 
      from ../main.cpp:13: 
../boostlibs/boost/asio/impl/io_service.hpp: In member function ‘void boost::asio::io_service::post(const CompletionHandler&) [with CompletionHandler = boost::packaged_task<bool>]’: 
../main.cpp:256:23: instantiated from here 
../boostlibs/boost/asio/impl/io_service.hpp:95:67: error: no matching function for call to ‘boost::packaged_task<bool>::packaged_task(const boost::packaged_task<bool>&)’ 
../boostlibs/boost/asio/impl/io_service.hpp:95:67: note: candidates are: 
../boostlibs/boost/thread/future.hpp:1372:9: note: boost::packaged_task<R>::packaged_task(boost::detail::thread_move_t<boost::packaged_task<R> >) [with R = bool] 
../boostlibs/boost/thread/future.hpp:1372:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::detail::thread_move_t<boost::packaged_task<bool> >’ 
../boostlibs/boost/thread/future.hpp:1318:9: note: boost::packaged_task<R>::packaged_task() [with R = bool] 
../boostlibs/boost/thread/future.hpp:1318:9: note: candidate expects 0 arguments, 1 provided 
../boostlibs/boost/thread/future.hpp:1314:9: note: boost::packaged_task<R>::packaged_task(boost::packaged_task<R>&) [with R = bool, boost::packaged_task<R> = boost::packaged_task<bool>] 
../boostlibs/boost/thread/future.hpp:1314:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::packaged_task<bool>&’ 
../boostlibs/boost/asio/detail/handler_type_requirements.hpp:95:26: error: initializing argument 1 of ‘T& boost::asio::detail::lvref(T) [with T = boost::packaged_task<bool>]’ 
../boostlibs/boost/asio/impl/io_service.hpp:95:67: error: no matching function for call to ‘boost::packaged_task<bool>::packaged_task(const boost::packaged_task<bool>&)’ 
../boostlibs/boost/asio/impl/io_service.hpp:95:67: note: candidates are: 
../boostlibs/boost/thread/future.hpp:1372:9: note: boost::packaged_task<R>::packaged_task(boost::detail::thread_move_t<boost::packaged_task<R> >) [with R = bool] 
../boostlibs/boost/thread/future.hpp:1372:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::detail::thread_move_t<boost::packaged_task<bool> >’ 
../boostlibs/boost/thread/future.hpp:1318:9: note: boost::packaged_task<R>::packaged_task() [with R = bool] 
../boostlibs/boost/thread/future.hpp:1318:9: note: candidate expects 0 arguments, 1 provided 
../boostlibs/boost/thread/future.hpp:1314:9: note: boost::packaged_task<R>::packaged_task(boost::packaged_task<R>&) [with R = bool, boost::packaged_task<R> = boost::packaged_task<bool>] 
../boostlibs/boost/thread/future.hpp:1314:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::packaged_task<bool>&’ 
../boostlibs/boost/asio/detail/handler_type_requirements.hpp:96:32: error: initializing argument 1 of ‘const T& boost::asio::detail::clvref(T) [with T = boost::packaged_task<bool>]’ 
../boostlibs/boost/asio/impl/io_service.hpp:97:3: error: no matching function for call to ‘boost::packaged_task<bool>::packaged_task(const boost::packaged_task<bool>&)’ 
../boostlibs/boost/asio/impl/io_service.hpp:97:3: note: candidates are: 
../boostlibs/boost/thread/future.hpp:1372:9: note: boost::packaged_task<R>::packaged_task(boost::detail::thread_move_t<boost::packaged_task<R> >) [with R = bool] 
../boostlibs/boost/thread/future.hpp:1372:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::detail::thread_move_t<boost::packaged_task<bool> >’ 
../boostlibs/boost/thread/future.hpp:1318:9: note: boost::packaged_task<R>::packaged_task() [with R = bool] 
../boostlibs/boost/thread/future.hpp:1318:9: note: candidate expects 0 arguments, 1 provided 
../boostlibs/boost/thread/future.hpp:1314:9: note: boost::packaged_task<R>::packaged_task(boost::packaged_task<R>&) [with R = bool, boost::packaged_task<R> = boost::packaged_task<bool>] 
../boostlibs/boost/thread/future.hpp:1314:9: note: no known conversion for argument 1 from ‘const boost::packaged_task<bool>’ to ‘boost::packaged_task<bool>&’ 
../boostlibs/boost/asio/detail/impl/task_io_service.hpp:54:6: error: initializing argument 1 of ‘void boost::asio::detail::task_io_service::post(Handler) [with Handler = boost::packaged_task<bool>]’ 

Utilizzando boost :: mossa (task) risultati nelle due errori:

error: no match for call to ‘(boost::detail::thread_move_t<boost::packaged_task<bool> >)()’ 
error: no match for call to ‘(boost::detail::thread_move_t<boost::packaged_task<bool> >)()’ 
+0

Quale versione Boost usi? – Rost

+1

In 1.50 boost :: move (attività) dovrebbe funzionare. –

+0

@Rost, @IgorR., Stavo usando 1.49, ora sto usando 1.51 e ottengo ancora il seguente errore quando uso boost :: move (task): '../boost_1_51_0/boost/asio/handler_invoke_hook.hpp:64: 3: errore: nessuna corrispondenza per la chiamata a '(boost :: detail :: thread_move_t >)()' 'Qualche idea? –

risposta

14

boost::packaged_task supporta boost::move dal Boost versione 1.50, vedi corrispondente ticket .

Ma il problema è che il parametro del gestore di completamento io_service::post deve essere CopyConstructible come indicato in Asio handler requirements. Pertanto, boost::packaged_task non può essere pubblicato direttamente o spostandosi. (Grazie a Igor R. per questo problema).

C'è una soluzione alternativa che utilizza i puntatori, ad es. si potrebbe avvolgere boost::packaged_task con boost::shared_ptr e associarlo al operator():

typedef boost::packaged_task<bool> task_t; 
boost::shared_ptr<task_t> task = boost::make_shared<task_t>(
    boost::bind(&process_data, i, theTime)); 

io_service.post(boost::bind(&task_t::operator(), task)); 
+0

il tuo work-around dell'operatore di binding() sul puntatore condiviso ha funzionato. il tuo aiuto con questo! –

+0

Sarei molto interessato a sapere come fanno queste fiere per le persone che usano VC++ 10. Mentre i compiti condivisi condivisi sembrano funzionare bene su posix (mac clang e linux gcc), ottengo degli errori molto strani su Windows -> 'funzione membro già definita o dichiarata' –

+0

@BenJ Ho appena compilato questo codice su VC2010 utilizzando Boost 1.53 e compila e funziona correttamente (tranne per l'avviso C4913 in future.hpp ma questo è noto problema Boost). per favore, pubblica il tuo codice effettivo? – Rost