int main( int argc, char **argv )
{
    // google::InitGoogleLogging(argv[0]);

    LOG(INFO) << "main thread " << THIS_THREAD_ID << " running...";

    // 如果没有,io_service 执行完了队列中所有缓存的job就退出,在getchar()之前terminating
    boost::asio::io_service::work io_work( std::ref(g_io_service) );

    boost::thread_group thrgrp;

    for (int i = 0; i < 5; ++i)
        thrgrp.create_thread( run_io_service );

    SLEEP_SECONDS(1);

    g_io_service.dispatch( std::bind(job_func, 1) );

    getchar();
    LOG(INFO) << "Trying to stop io_service...";

    // NOTE!!! 如果job_func里用dispatch就停不下来,嵌套
    g_io_service.stop();

    thrgrp.join_all();

    return 0;
}
Exemple #2
0
/**
 * @brief Funkcia sa snazi co najbezpecnejsim sposobom ukoncit server v pripade nudze
 *
 * zastavuje celu sietovu vrstvu programu a uvolnuje pamat
 */
static void sighandler(int signum) 
{ 
    (void)signum;
    io_service.reset();
    io_service.stop();
    Manager::instance().Shutdown();
}
Exemple #3
0
void ShutdownThreadPool(std::vector<std::thread>& threadPool)
{
    _ioService.stop();

    for (auto& thread : threadPool)
        thread.join();
}
    void freceive_more_async(boost::asio::io_service & ios, aziomq::socket & socket,
                             const const_buf_vec & expected_bufs, int flags) {
        SYNC_LOG(__PRETTY_FUNCTION__);
         //create vector of raw bufs to fill from length of expected_bufs
        buf_vec_t buf_vec(std::distance(std::begin(expected_bufs),
                                        std::end(expected_bufs)));
        zero(buf_vec);

         //create azio buffer vector
        mutable_buf_vec bufs;
        init(bufs, buf_vec);

        auto it = std::begin(bufs);
        auto e = std::end(bufs);

        std::exception_ptr err;
        socket.async_receive_more(boost::asio::buffer(*it++),
                [&ios, &socket, &bufs, &it, e, &err, expected_bufs](const boost::system::error_code & ec,
                                                                   aziomq::socket::more_result mr) {
                    if (ec) {
                        err = std::make_exception_ptr(boost::system::system_error(ec));
                    } else {
                        for (; mr.second && it != e; ++it) {
                            mr = socket.receive_more(boost::asio::buffer(*it));
                            err = check_res(bufs, expected_bufs);
                        }
                    }
                    ios.stop();
                });
        ios.run();
        if (err != std::exception_ptr())
            std::rethrow_exception(err);
    }
    void freceive_async(boost::asio::io_service & ios, aziomq::socket & socket,
                            const const_buf_vec & expected_bufs, int flags) {
        SYNC_LOG(__PRETTY_FUNCTION__);
         //create vector of raw bufs to fill from length of expected_bufs
        buf_vec_t buf_vec(std::distance(std::begin(expected_bufs),
                                        std::end(expected_bufs)));
        zero(buf_vec);

         //create azio buffer vector
        mutable_buf_vec bufs;
        init(bufs, buf_vec);

        std::exception_ptr err;
        socket.async_receive(bufs,
                [&ios, &bufs, &err, expected_bufs](const boost::system::error_code & ec, size_t bytes_transferred) {
                    if (ec) {
                        err = std::make_exception_ptr(boost::system::system_error(ec));
                    } else {
                        err = check_res(bufs, expected_bufs);
                    }
                    ios.stop();
                });
        ios.run();
        if (err != std::exception_ptr())
            std::rethrow_exception(err);
    }
Exemple #6
0
BOOL CtrlHandler(DWORD fdwCtrlType)
{
	switch (fdwCtrlType)
	{
		// Handle the CTRL-C signal. 
	case CTRL_C_EVENT:
		printf("Ctrl-C event\n\n");
		Beep(750, 300);
		io_service.stop();
		return(TRUE);

		// CTRL-CLOSE: confirm that the user wants to exit. 
	case CTRL_CLOSE_EVENT:
		Beep(600, 200);
		printf("Ctrl-Close event\n\n");
		io_service.stop();
		return(TRUE);

		// Pass other signals to the next handler. 
	case CTRL_BREAK_EVENT:
		Beep(900, 200);
		printf("Ctrl-Break event\n\n");
		io_service.stop();
		return FALSE;

	case CTRL_LOGOFF_EVENT:
		Beep(1000, 200);
		printf("Ctrl-Logoff event\n\n");
		io_service.stop();
		return FALSE;

	case CTRL_SHUTDOWN_EVENT:
		Beep(750, 500);
		printf("Ctrl-Shutdown event\n\n");
		io_service.stop();
		return FALSE;

	default:
		return FALSE;
	}
	/*
	boost::asio::signal_set signals(io_service, SIGINT, SIGTERM);
	signals.async_wait(
		boost::bind(&boost::asio::io_service::stop, &io_service));//*/
}
Exemple #7
0
void stopLogThread()
{
    if (LogThreadWork.get())
    {
        LogThreadWork.reset();
        LogIoService.stop();
        LogThread.try_join_for(boost::chrono::milliseconds(100));
    }
}
Exemple #8
0
void subscribeHandler(boost::asio::io_service &ioService, const std::vector<char> &buf)
{
    std::string msg(buf.begin(), buf.end());

    std::cerr << "Message: " << msg << std::endl;

    if( msg == "stop" )
        ioService.stop();
}
Exemple #9
0
void ShutdownThreadPool(std::vector<std::thread>& threadPool)
{
    sScriptMgr->OnNetworkStop();

    _ioService.stop();

    for (auto& thread : threadPool)
    {
        thread.join();
    }
}
 void fsend_async(boost::asio::io_service & ios, aziomq::socket & socket, const const_buf_vec & bufs, int flags) {
     SYNC_LOG(__PRETTY_FUNCTION__);
     std::exception_ptr err;
     socket.async_send(bufs,
             [&ios,&err](const boost::system::error_code & ec, size_t bytes_transferred) {
                 if (ec)
                     err = std::make_exception_ptr(boost::system::system_error(ec));
                 ios.stop();
             });
     ios.run();
     if (err != std::exception_ptr())
         std::rethrow_exception(err);
 }
Exemple #11
0
void SignalHandler(const boost::system::error_code& error, int signalNumber)
{
    if (!error)
    {
        switch (signalNumber)
        {
        case SIGINT:
        case SIGTERM:
            _ioService.stop();
            break;
        }
    }
}
Exemple #12
0
int main()
{
    boost::shared_ptr< boost::asio::io_service::work > work(new boost::asio::io_service::work(io_service));
    boost::thread_group worker_threads;
    Debug("Press [Enter] to exit.");
    for(int x = 0; x < 4;x++){
        worker_threads.create_thread(WorkerThread);
    }
    std::cin.get();
    io_service.stop();
    worker_threads.join_all();
    return 0;
}
inline void AsioThreadPool::stop()
{
    m_work.reset();

    m_io_svc.stop();

    for (auto &i : m_threads)
    {
        if (i.joinable())
        {
            i.join();
        }
    }
}
Exemple #14
0
void read_images() {
    Frame* currentFrame;
    while (hasMoreFrames) {
        Frame* f = importer.next_frame();
        if (f) {
            in_buffer.push(f);
        }
        else {
            hasMoreFrames = false;
            ioService.stop();
        }
        boost::this_thread::sleep(boost::posix_time::milliseconds(30));
    }
}
int main(int argc, char** argv)
{
  if (2 > argc) {
    std::cout << "\nusage: " << argv[0] << " <term to track> [--full_screen]\n\n";
    return 1;
  }

  tw_term = argv[1];
  std::thread t1(setup_network_stream);

  draw_window(3 == argc);

  io_service.stop();
  t1.detach();

  return 0;
}
Exemple #16
0
// 
//   FUNCTION: CSampleService::OnStop(void) 
// 
//   PURPOSE: The function is executed when a Stop command is sent to the  
//   service by SCM. It specifies actions to take when a service stops  
//   running. In this code sample, OnStop logs a service-stop message to the  
//   Application log, and waits for the finish of the main service function. 
// 
//   COMMENTS: 
//   Be sure to periodically call ReportServiceStatus() with  
//   SERVICE_STOP_PENDING if the procedure is going to take long time.  
// 
void AQService::OnStop() 
{ 

	m_io_service.stop();

	// Log a service stop message to the Application log. 
	WriteEventLogEntry(L"CppWindowsService in OnStop",  
		EVENTLOG_INFORMATION_TYPE); 

	// Indicate that the service is stopping and wait for the finish of the  
	// main service function (ServiceWorkerThread). 
	m_fStopping = TRUE; 
	if (WaitForSingleObject(m_hStoppedEvent, INFINITE) != WAIT_OBJECT_0) 
	{ 
		throw GetLastError(); 
	} 
}
Exemple #17
0
   ~thread_pool()
   {
      DLOG(INFO) << "stopping service...";
      // Force all threads to return from io_service::run().
      io_service_.stop();

      DLOG(INFO) << "joining threads...";
      try
      {
         threads_.join_all();
      }
      catch (...)
      {
         LOG_EXCEPTION();
      }
      DLOG(INFO) << __func__;
   }
Exemple #18
0
void Run(int id, int count)
{
	for (size_t i = 0; i < count; ++i)
	{
		if (isRun)
		{
			std::cout << "id = " << id << "\n";
			Sleep(1000);
		}
		else
		{
			break;
		}
	}
	timer.cancel();
	io.stop();
	io.reset();
}
void signal_received(const bs::error_code &ec, int number)
{
	if (!ec) {
		if (tcp_socket != NULL)
			delete tcp_socket;
		
		set<Client*>::iterator it;
		vector<Client*> clients_to_remove;
		
		for (it = clients.begin(); it != clients.end(); ++it)
			clients_to_remove.push_back(*it);
		
		while (!clients_to_remove.empty()) {
			remove_client(clients_to_remove.back());
			clients_to_remove.pop_back();
		}
		
		io_service.stop();
	}
}
Exemple #20
0
/*****************************************************************************
*   listening server
*****************************************************************************/
void server( boost::asio::io_service & io_svc, tcp::acceptor & a) {
    print( tag(), ": echo-server started");
    try {
        for (;;) {
            socket_ptr socket( new tcp::socket( io_svc) );
            boost::system::error_code ec;
            a.async_accept(
                    * socket,
                    boost::fibers::asio::yield[ec]);
            if ( ec) {
                throw boost::system::system_error( ec); //some other error
            } else {
                boost::fibers::fiber( session, socket).detach();
            }
        }
    } catch ( std::exception const& ex) {
        print( tag(), ": caught exception : ", ex.what());
    }
    io_svc.stop();
    print( tag(), ": echo-server stopped");
}
Exemple #21
0
void handleConnected(boost::asio::io_service &ioService, RedisClient &redis,
        bool ok, const std::string &errmsg)
{
    if( ok )
    {
        redis.command("SET", redisKey, redisValue, [&](const RedisValue &v) {
            std::cerr << "SET: " << v.toString() << std::endl;

            redis.command("GET", redisKey, [&](const RedisValue &v) {
                std::cerr << "GET: " << v.toString() << std::endl;

                redis.command("DEL", redisKey, [&](const RedisValue &) {
                    ioService.stop();
                });
            });
        });
    }
    else
    {
        std::cerr << "Can't connect to redis: " << errmsg << std::endl;
    }
}
Exemple #22
0
static void process_signals()
{
    sigset_t mask;
    sigemptyset(&mask);
    sigaddset(&mask, SIGCHLD);
    sigaddset(&mask, SIGPIPE);
    sigaddset(&mask, SIGUSR1);
    sigaddset(&mask, SIGUSR2);
    sigaddset(&mask, SIGTSTP);
    sigaddset(&mask, SIGTTIN);
    sigaddset(&mask, SIGHUP);
    if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
        fmt::print(stderr, "sigprocmask failed\n");
        std::exit(EXIT_FAILURE);
    }
    asio_signal_set.add(SIGINT);
    asio_signal_set.add(SIGTERM);
    asio_signal_set.async_wait(
        [](const boost::system::error_code &, int signum) {
            io_service.stop();
        });
}
Exemple #23
0
int main()
{
	for (size_t i = 0; i < 5; ++i)
	{
		std::cout << "Iteraton = " << i << "\n";

		timer.cancel();
		io.stop();
		io.reset();
		timer.expires_from_now(boost::posix_time::seconds(200));

		isRun = true;
		boost::thread th(Timing);

		Run(i, 5);

		th.join();
	}



	return 0;
}
Exemple #24
0
void SignalHandler(const boost::system::error_code& error, int /*signalNumber*/)
{
    if (!error)
        _ioService.stop();
}
Exemple #25
0
 void stop(){
     _ios.stop();
 }
	// Methods
	void onStop() {
		mIoService.stop();
	}
		~Loader() {
			// stop & wait for the thread to finish
			io.stop();
			if( joinable() )
				thread->join();
		}
Exemple #28
0
 void terminate() {
     io_service_.stop();
     for ( auto& t : threads_ )
         t.join();
 }
	void stop()
	{
		ios_.stop();
	}
Exemple #30
0
amxSocket::~amxSocket()
{
	gDebug->Log("Socket deconstructor called");

	gIOService.stop();
}