同步调用的执行是一个有序的线性序列,当 A 调用 B 后,A 会主动等待 B 执行完成后再继续。比如 A 先后两次调用 B,记为 B(1),B(2),执行顺序一定是:“A call B(1) -> B(1) run -> B(1) done, return A -> A call B(2) -> B(2) run -> B(2) done, return A -> A continue”只有按照这种线性序列去执行程序,才能保证调用双方的状态同步。
异步调用是指当 A 调用 B 的时候,不等 B 执行结束,这个调用就会立即返回。当 B 执行完成后,B 会通过回调函数或通知 的方式告诉 A。异步调用并不是 FIFO 的,即 A 发起的多个调用并不是按照顺序收到通知的,有可能 A 后发出的调用却先收到了 B 的回应。
recv_packet: type is 2 connect to server successful! main thread: thread running session_state is 1 ack_handler thread: thread started! ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 26869 and 26972 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 22789 and 22892 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 18710 and 18812 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 14630 and 14732 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 10549 and 10652 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 6469 and 6572 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 2390 and 2493 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 0 and 0 sent pingreq to server successful! recv_packet: type is 13 ack_handler thread: try to recv packet type is 13 ack_handler thread: keep alive timer remain 30911 and 30999 ack_handler thread: received PINGRESP successful! main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 26840 and 26928 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 22760 and 22848 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 18680 and 18768 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 14600 and 14688 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 10520 and 10608 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 6440 and 6528 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 2360 and 2448 main thread: thread running session_state is 1 ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 0 and 0 sent pingreq to server successful! recv_packet: type is 13 ack_handler thread: try to recv packet type is 13 ack_handler thread: keep alive timer remain 30908 and 30999 ack_handler thread: received PINGRESP successful! ack_handler thread: try to recv packet type is -1 ack_handler thread: keep alive timer remain 26840 and 26931 main thread: thread running session_state is 1