老师,我试验了程序,发现有一个问题。
服务器程序启动后输出结果与文章中的不一样。
./poll-server-multithreads
[msg] set poll as dispatcher, main thread
[msg] add channel fd == 4, main thread
[msg] poll added channel fd==4, main thread
[msg] set poll as dispatcher, Thread-1
[msg] add channel fd == 8, Thread-1
[msg] poll added channel fd==8, Thread-1
[msg] event loop thread init and signal, Thread-1
[msg] event loop run, Thread-1
[msg] event loop thread started, Thread-1
[msg] set poll as dispatcher, Thread-2
[msg] add channel fd == 10, Thread-2
[msg] poll added channel fd==10, Thread-2
[msg] event loop thread init and signal, Thread-2
[msg] event loop run, Thread-2
[msg] event loop thread started, Thread-2
[msg] set poll as dispatcher, Thread-3
[msg] add channel fd == 19, Thread-3
[msg] poll added channel fd==19, Thread-3
[msg] event loop thread init and signal, Thread-3
[msg] event loop run, Thread-3
[msg] event loop thread started, Thread-3
[msg] set poll as dispatcher, Thread-4
[msg] add channel fd == 21, Thread-4
[msg] poll added channel fd==21, Thread-4
[msg] event loop thread init and signal, Thread-4
[msg] event loop run, Thread-4
[msg] event loop thread started, Thread-4
[msg] add channel fd == 6, main thread
[msg] poll added channel fd==6, main thread
[msg] event loop run, main thread
各个子线程启动后创建的套接字对是添加在子线程的eventloop上的,而不是像文章中的全是添加在主线程中。
从我阅读代码来看,确实也是添加在子线程中。不知道哪里不对?
主线程给子线程下发连接套接字是通过主线程调用event_loop_add_channel_event完成的,当主线程中发现eventloop和自己不是同一个线程,就通过给这个evenloop的套接字对发送一个“a”产生事件唤醒,然后子线程处理pending_channel,实现在子线程中添加连接套接字。
展开
作者回复: 我怎么觉的你的结果是对的呢?有可能我文章中贴的信息不够全,造成了一定的误导。