#include #include #include #include #include using namespace boost; using namespace std; cobalt::generator genere(int id, int start, int end, int waitsec) { auto ctx = co_await asio::this_coro::executor; for (int i=start; i 0){ asio::steady_timer tim{ctx,std::chrono::seconds(waitsec)}; co_await tim.async_wait(cobalt::use_op); } co_yield i; } co_return end; } #include "../trunk/getnow.h" cobalt::promise print(int id, int nbrep, int waitsec) { auto g = genere(id,1,nbrep,waitsec); cout << "debut print\n"; if (nbrep > 1000){ auto start = getnow(); int total = 0; while (g){ int val = co_await g; total += val; } auto end = getnow(); showtime ("print",start,end); cout << format ("{} par seconde\n",getnow_formatnum((double)nbrep/(end-start)*1000000)); cout << format ("total = {}\n",total); }else{ while (g){ //cout << format("print id={} co_await\n",id); int val = co_await g; cout << format ("print id={} val={}\n",id,val); } } cout << "fin print\n"; co_return; } cobalt::task callprint (int id, int nbrep, int waitsec) { co_await print (id,nbrep,waitsec); } #include "../trunk/c++script.h" cobalt::main co_main(int argc, char ** argv) { cpps::progdesc ("Prototype pour comprendre cobalt"); cpps::options_section("Variations"); cpps::option verbose ('v',"verbose","Affiche debug",false,false); cpps::option waitsec ('w',"waitsec","Secondes à atendre dans le test range",0,false); cpps::option nbrep ('n',"nbrep","Nombre de répétitions",5,false); cpps::option nbpar ('P',"nbpar","Nombre de processus en parallèle",1,false); cpps::option group ('G',"group","Utilise wait_group",false,false); cpps::endoptions(argc,argv); { int total=0; for (int i=1; i<=nbrep.val; i++) total += i; cout << format ("nbrep={} total={}\n",nbrep.val,total); } cobalt::run(callprint(0,nbrep.val,waitsec.val)); auto ctx = co_await cobalt::this_coro::executor; asio::signal_set signals(ctx, SIGINT, SIGTERM); signals.async_wait([](auto ec, int signum){ cerr << format ("signal number = {}\n",signum); cerr << "Exception " << ec << endl; exit (-1); }); if (group.val){ cobalt::wait_group workers; for (int i=1; i<=nbpar.val; i++){ cout << format ("Avant wait_group {}\n",i); workers.push_back(print(i,nbrep.val,waitsec.val)); } while (workers.size() > 0){ cout << format ("workers.size()={}\n",workers.size()); co_await workers.wait_one(); } }else{ for (int i=1; i<=nbpar.val; i++){ cout << format ("Avant spawn {}\n",i); cobalt::spawn(ctx,callprint(i,nbrep.val,waitsec.val),asio::detached); } } cout << "Avant run\n"; //asio::steady_timer tim{ctx,std::chrono::seconds(10)}; //co_await tim.async_wait(cobalt::use_op); //ctx.run(); co_return 0u; }