Qihoo360 / evpp

A modern C++ network library for developing high performance network services in TCP/UDP/HTTP protocols.
BSD 3-Clause "New" or "Revised" License
3.59k stars 943 forks source link

How do I exit loop.run()? #59

Closed caistd closed 7 years ago

caistd commented 7 years ago

// @brief Join the thread. If you forget to call this method, // it will be invoked automatically in the destruct method ~EventLoopThread(). // @note DO NOT call this method from any of the working thread. void Join();

EventLoopThread::~EventLoopThread() { DLOG_TRACE << "loop=" << eventloop; assert(IsStopped()); Join(); } Causes the thread to fail

zieckey commented 7 years ago

Use loop.Stop() to exit. For example:

    auto f = [&loop]() {
        loop.Stop();
    };
    loop.RunAfter(evpp::Duration(10.0), f);
caistd commented 7 years ago

Is there a manual?

zieckey commented 7 years ago

Sorry, we don't have a manual. Please read the example source codes.

caistd commented 7 years ago

loop.RunAfter(evpp::Duration(1.0), [&loop] { loop.Stop(); });
is ok auto f = [&loop]() { loop.Stop(); }; loop.RunAfter(evpp::Duration(10.0), f); [ &loop] lambda Unable to capture variables why

zieckey commented 7 years ago
auto f = &loop {
    loop.Stop();
};

That lambda's format is wrong. The right way to define a lambda is :

    auto f = [&loop]() {
        loop.Stop();
    };
caistd commented 7 years ago

thankyou Is there any other way to solve it? and What's the difference between event_loop_thread.h and event_loop_thread_pool.h in multiple threads ?

zieckey commented 7 years ago

EventLoopThread is one thread, EventLoopThreadPool is a thread pool which holds multi threads.

caistd commented 7 years ago

How to create multithreaded?

zieckey commented 7 years ago

Please see the API. e.g. evpp::TCPServer(&loop, addr, "TCPEcho", 12) will create a thread pool with 12 threads in it.

zieckey commented 7 years ago

what is PostTask?

caistd commented 7 years ago
// @brief Join the thread. If you forget to call this method,
// it will be invoked automatically in the destruct method ~EventLoopThread().
// @note DO NOT call this method from any of the working thread.
void Join();

EventLoopThread::~EventLoopThread() { DLOG_TRACE << "loop=" << eventloop; assert(IsStopped()); Join(); }

Causes the thread to fail