
xutm wrote:
If i want to kill a thread which is no longer useful, I think i just need to call the two lines: " thrd->interrupt();" and " thrd->detach();" . But actully, the two lines cannot kill the thread.
POSIX defines a thread cancellation scheme in which a thread that is blocked doing i/o can be killed, however nether Boost.Thread nor the forthcoming C++0x std::thread allow this. If you are running on a POSIX platform you can avoid those C++ thread libraries and manage threads directly via the POSIX API, but in that case you have the question of how cleanly the killed thread will terminate i.e. will it call destructors. In general it will not do so, but GNU libc does call destructors in this case. So you can get the behaviour that you want if you are running on a GNU libc platform and if you're prepared to write your own thread class. Otherwise, threads that are blocking doing i/o are unkillable. One work-around is to have a single thread doing all i/o using e.g. select() to avoid blocking, and transfering data to and from the other threads using condition variables. Those threads will block waiting on the condition variables, and that blocking is interruptable with Boost.Thread. (I don't know much about Boost.asio, but I would guess that it has support for this sort of thing.) Or, just don't use threads. Regards, Phil.