muduo 3

one concern yesterday: does each worker thread call loop() and run the whole active channels callback ?

suppose no. let’s track the process :

1
2
3
4
5
6
tcpServer.start() ->
threadPool.start() ->
new EventLoopThread(eventLoopThread::threadFunc, threadName)
eventLoopThread->startLoop() ->
thread.start() -> detail::startThread() ->
runInThread()::func_()

the real func_ during creating new eventLoopThread, is threadFunc(), in which call loop.loop().

first, this loop object is created inside this new eventLoopThread object, no issue that the loop.threadId_ is current threadID.

when multiple worker threads exist, what happens ?

threads A B C
conns T1 T2 T3

suppose T1 is assigend to worker thread A, etc. when calling eventLoopThread A.loop(), epoll_wait() is first called and return number of active events, epoll_wait() is thread-safe, no worry.

then activeChannels:HandleEvents() is called. the real executor of this handleEvents is in the TcpConnection objects, namely: T1 or T2 or T3.

each TcpConnection object has its own loop-, and only the worker thread with the same loop- can execute the events. so even though threadA get the list of all active events, but only events in T1 conenction will be executed by threadA.

so we can see, in per (worker) thread per epoll, each (worker) epoll looks like only return the active channels/events in current worker thread’s eventLoop. There is no conflict among multiple worker threads, since each worker threas has its own eventLoop.

how about eventLoop in server/client

the eventLoopThread is kind of triggered inside Muduo, where is Tcp server/client eventLoop triggered ? from outsiders.