SN-10103 附来源、原文与上下文。
test 5:
test 5:
我在 send 和 recv 调用中加了 MSG_DONTWAIT,以防它们忘了套接字是非阻塞的。如果还不行,现在还有一个兜底办法:用另一个线程监控 send/recv 线程,若它停止运行,就终止并重启它。程序会在 debug.log 中打印“*** Restarting ThreadSocketHandler ***”,状态栏也会显示一段时间的错误消息。
I added MSG_DONTWAIT to the send and recv calls in case they forgot the socket is non-blocking. If that doesn't work, there's now the catch-all solution: another thread monitors the send/recv thread and terminates and restarts it if it stops. It prints "*** Restarting ThreadSocketHandler ***" in debug.log, and an error message displays on the status bar for a while.
在终止之前,它会先尝试关闭卡住的套接字。如果成功,就不必采取终止线程这一步了。
Before terminating, it tries closing the socket that's hung. If that works, it doesn't have to resort to terminating.
我做了一次测试,终止线程大约 1000 次,没有出问题,所以应该安全。linux 上用于终止的是 pthread_cancel,它会让执行进入 C++ 的异常处理程序。
I ran a test where it terminated the thread about 1000 times without trouble, so it should be safe. The terminate on linux is pthread_cancel, which throws it into C++'s exception handler.
我们之前使用的线程调用没有终止功能,所以我在 util.h 中创建了自己的封装,在 windows 上使用 CreateThread,在 linux 上使用 pthread_create,而不是: _beginthread 仅适用于 windows,而且没有终止功能 boost::thread 很吸引人,但没有终止功能 wxThread 要求你为每个可能调用的函数创建一个类(真糟糕)
The thread calls we were using didn't have terminate, so I created our own wrappers in util.h to use CreateThread on windows and pthread_create on linux, instead of: _beginthread is windows only and lacks terminate boost::thread is really attractive, but lacks terminate wxThread requires you to create a class for every function you might call (yuck)
文件附在下一封邮件中。
File attached in the next e-mail