void AsyncDBCreateGame::HandleNoResult(mysqlpp::Query &query, DBIdManager& idManager, boost::asio::io_service &service, ServerDBCallback &cb) { query.reset(); query << "SELECT LAST_INSERT_ID()"; mysqlpp::StoreQueryResult tmpResult = query.store(); DB_id insertId = tmpResult[0][0]; if (!tmpResult || tmpResult.num_rows() != 1 || insertId == 0) { service.post(boost::bind(&ServerDBCallback::CreateGameFailed, &cb, GetId())); } else { service.post(boost::bind(&ServerDBCallback::CreateGameSuccess, &cb, GetId())); idManager.AddGameId(GetId(), insertId); } }
bool simulator::acquire( boost::asio::io_service& io_service ) { hasWaveform_ = false; if ( ! acqTriggered_ ) { io_service.post( [&]() { if ( __waveform_generator_generator ) { if ( auto generator = __waveform_generator_generator( sampInterval_, startDelay_, nbrSamples_, nbrWaveforms_ ) ) { generator->addIons( ions_ ); generator->onTriggered(); std::this_thread::sleep_for( std::chrono::milliseconds( nbrWaveforms_ ) ); // simulate triggers post( generator.get() ); hasWaveform_ = true; std::unique_lock< std::mutex > lock( queue_ ); cond_.notify_one(); } } } ); acqTriggered_ = true; return true; } return false; }
void push_job(int seconds, boost::asio::io_service& io_service, std::vector<boost::shared_future<int> >& pending_data) { boost::shared_ptr<task_t> task = boost::make_shared<task_t>(boost::bind(&sleep_print, seconds)); boost::shared_future<int> fut(task->get_future()); pending_data.push_back(fut); io_service.post(boost::bind(&task_t::operator(), task)); }
static void iopost_msg(boost::asio::io_service & io_service, boost::function<void(std::string)> msg_sender, std::string msg, std::string groupid) { io_service.post(boost::bind(msg_sender, msg)); logfile.add_log(groupid, msg); }
Server(boost::asio::io_service& io_service) : io_service(io_service), data_strand(io_service), active(0), sock(io_service, udp::endpoint(udp::v4(), 12321)), recv_timer(io_service), send_timer(io_service), msg_timer(io_service) { for(auto& p : players){ p.present = false; p.pos = glm::vec3(1.0, 1.0, 1.0); } start_receive(); io_service.post( data_strand.wrap(std::bind(&Server::respond, this))); io_service.post( data_strand.wrap(std::bind(&Server::broadcast, this))); io_service.post( data_strand.wrap(std::bind(&Server::send_msg, this))); }
std::pair<size_t, size_t> push(pair_type && pair, boost::asio::io_service & main_service) { auto empty = _sessions.empty(); _sessions.push(std::move(pair)); if (empty) main_service.post(std::bind(&auth_queue::next, this)); return { _sessions.size(), _counter }; }
void avlog_do_search(boost::asio::io_service & io_service, std::string c, std::string q, std::string date, boost::function<void (boost::system::error_code, pt::ptree)> handler, soci::session & db) { pt::ptree outjson; std::string q_escaped; // 根据 channel_name , query string , date 像数据库查找 AVLOG_DBG << " c = " << c << " q = " << q << " date= " << date ; std::vector<std::string> r_date(1000); std::vector<std::string> r_channel(1000); std::vector<std::string> r_nick(1000); std::vector<std::string> r_message(1000); std::vector<std::string> r_rowid(1000); avhttp::detail::unescape_path(q, q_escaped); boost::timer::cpu_timer cputimer; cputimer.start(); db << "select date,channel,nick,message,rowid from avlog where channel=:c " "and message like \"%" << q_escaped << "%\" order by strftime(`date`) DESC" , soci::into(r_date) , soci::into(r_channel) , soci::into(r_nick) , soci::into(r_message) , soci::into(r_rowid) , soci::use(c); pt::ptree results; // print out the result for (int i = 0; i < r_date.size() ; i ++) { pt::ptree onemsg; onemsg.put("date", r_date[i]); onemsg.put("channel", r_channel[i]); onemsg.put("nick", r_nick[i]); onemsg.put("channel", r_channel[i]); onemsg.put("message", r_message[i]); onemsg.put("id", r_rowid[i]); results.push_back(std::make_pair("", onemsg)); } outjson.put("params.num_results", r_date.size()); outjson.put_child("data", results); outjson.put("params.time_used", boost::timer::format(cputimer.elapsed(), 6, "%w")); io_service.post( boost::asio::detail::bind_handler(handler, boost::system::error_code(), outjson ) ); };
void Logger::finalizeLogRecord() { if (!recordsQueue_.push(currentLogRecord_.str())) sink_->finalizeRecord("[CRITICAL]\tlog queue is full"); // getOutFileStream() << "[CRITICAL]\tlog queue is full" << std::endl; else LogIoService.post(boost::bind(&Logger::processLogRecords, shared_from_this())); }
void worker(int id) { while (true) { this_thread::sleep_for(chrono::milliseconds(rand() % 3000)); shared_queue.post([id] { std::cout << "event from thread #" << id << " handled on " << this_thread::get_id << "\n"; }); } }
/* * NOTE!!! post dispatch 区别仅仅在io_service调用的工作函数里使用才看得出区别。 * dispatch 只有start没有finish */ static void job_func(int i) { LOG(INFO) << "Job " << i << " started in thread " << THIS_THREAD_ID; SLEEP_SECONDS(2); // g_io_service.dispatch(std::bind(job_func, i+1)); g_io_service.post(std::bind(job_func, i+1)); LOG(INFO) << "Job " << i << " finished in thread " << THIS_THREAD_ID; }
int main(int argc, char** argv) { init(); if (handle_args(argc, argv) == 1) return 0; int processors = boost::thread::hardware_concurrency(); ioService.post(boost::bind(read_images)); ioService.post(boost::bind(assign_workers)); ioService.post(boost::bind(output)); boost::asio::io_service::work work(ioService); for (int i = 0; i < processors; i++) { threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService)); } threadpool.join_all(); return 0; }
void syncLoad(AssetPath path, AssetPromisePtr pr, AssetContentPtr c) { Progress::Work w = (Progress::Work)boost::filesystem::file_size(path); mngr.prog_.addWork( w ); // add the loading function to the io_service io.post( boost::bind( &AssetManager::Loader::asyncLoad, this, path, pr, c, w ) ); }
// workarround windows that can use posix stream for stdin static void input_thread(boost::asio::io_service & io_service) { while (!boost::this_thread::interruption_requested() && !std::cin.eof()) { std::string line; std::getline(std::cin, line); io_service.post([line]{ input_got_one_line(ansi_utf8(line)); }); } }
static void worker() { sleep(2); svc.post([](){ std::cout<<"\nRaising signal."; std::string hello("hello world"); textEntered(hello); }); return; }
int main(int argc, char **argv) { if (argc != 3) { std::cerr << "usage stackless_coroutine_perf <port> <read buffer size>\n"; return 1; } boost::asio::io_service io; auto work = std::make_unique<boost::asio::io_service::work>(io); std::promise<void> done_promise; auto done_future = done_promise.get_future(); auto func = [&]() { do_coroutine(io, "127.0.0.1", argv[1], std::stoi(argv[2]), [&](auto &value, std::exception_ptr e, auto op) { if (e) { done_promise.set_exception(e); } else { auto end_time = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>( end_time - value.start_time); std::cout << "Received " << value.bytes << " in " << duration.count() << " milliseconds\n"; std::cout << "Transmit speed " << (value.bytes / duration.count()) * 1000 << " Bytes/Seconds \n"; done_promise.set_value(); } }); }; io.post(func); auto frun = std::async(std::launch::async, [&]() { io.run(); }); done_future.wait(); work.reset(); frun.get(); try { done_future.get(); } catch (std::exception &e) { std::cerr << e.what() << "\n"; } };
void async_rename(const boost::filesystem::path & source, const boost::filesystem::path & destination, Callback callback) { worker_service_.post(boost::bind( &filesystem_service::rename<boost::_bi::protected_bind_t<Callback> >, this, boost::make_shared<boost::asio::io_service::work>(boost::ref(io_service_)), source, destination, boost::protect(callback) )); }
void assign_workers() { Frame* current; while (hasMoreFrames || in_buffer.size() > 0) { if (in_buffer.size() > 0) { current = in_buffer.front(); // spawn worker to process image; BOOST_LOG_TRIVIAL(info) << "Working..."; ioService.post(boost::bind(worker, current)); in_buffer.pop(); } boost::this_thread::sleep(boost::posix_time::milliseconds(30)); } }
// ------------------------------------------------------------------ // // @details This is a template method pattern. The completion calls // into the derived class to allow it to implement its custom code // following the completion of the computation. Once that is done, // this method sends the result and sends a new task request back to // the task generation source. // // ------------------------------------------------------------------ void Task::complete(boost::asio::io_service& ioService) { // // Call into the derived class and let it do whatever it wants first auto message = this->completeCustom(ioService); // // Post the message to the io_service to be returned back to the client. std::shared_ptr<ip::tcp::socket> socket = m_socket; ioService.post( [message, socket]() { message->send(socket); }); // // We send a request for more work ioService.post( [socket]() { Messages::TaskRequest command; command.send(socket); }); }
void generate_jobs() // we're on the data thread { try { for(;;) { boost::this_thread::sleep_for(boost::chrono::milliseconds(200)); boost::this_thread::interruption_point(); std::cout << '.' << std::flush; static uint64_t id = 0; Command cmd(++id); iosvc.post(boost::bind(&Server::dispatch, this, cmd)); // to the IO thread! } } catch(boost::thread_interrupted const&) { std::cout << "generate_jobs has been stopped" << std::endl; } }
service( boost::asio::io_service & io_svc) : boost::asio::io_service::service( io_svc), work_{ new boost::asio::io_service::work( io_svc) } { io_svc.post([&io_svc](){ //] //[asio_rr_service_lambda while ( ! io_svc.stopped() ) { if ( boost::fibers::has_ready_fibers() ) { // run all pending handlers in round_robin while ( io_svc.poll() ); // run pending (ready) fibers this_fiber::yield(); } else { // run one handler inside io_service // if no handler available, block this thread if ( ! io_svc.run_one() ) { break; } } } //] //[asio_rr_service_bottom }); }
static void post_to_io_queue( boost::asio::io_service& queue, const boost::function< void() >& f ) { queue.post( f ); }
//--------------------------------------------------------- void thread_00() { global_io_service.post(boost::bind(check_thread_id_00, boost::this_thread::get_id())); }
void AsyncDBLogin::HandleError(boost::asio::io_service &service, ServerDBCallback &cb) { service.post(boost::bind(&ServerDBCallback::QueryError, &cb, "AsyncDBLogin: Failure.")); }
static void defered_update_node( pubsub::root& root, const pubsub::node_name& node, const json::value& val, boost::asio::io_service& queue ) { queue.post( boost::bind( update_node, boost::ref( root ), node, val ) ); }
inline void push_task(const T& task_unwrapped) { ios_.post(detail::make_task_wrapped(task_unwrapped)); }
void post(Task task) { io_service_.post( boost::bind(task)); }
//--------------------------------------------------------- void thread_01() { global_io_service.post(run_00); }
void service_print4() { // PRINT_DEBUG("Enter print4"); m_service.post(boost::bind(print, 4)); // PRINT_DEBUG("Exit print4"); }
void await_suspend(co2::coroutine<> const& cb) { io.post(cb); }
void add_busy_work(boost::asio::io_service& io_service, unsigned int count) { io_service.post(boost::bind(busy_work, boost::ref(io_service), count)); }