Reputation: 5251
Well I don't have a specifc example to share however the question remains.
Say I have a main process Main
that creates a thread, AKA Thread
.
Thread
operates as follow (just an example):
What if "some process" is Main
and Thread
kills it?
killing the process may vary in method:
TerminateProcess
PostMessage
Upvotes: 0
Views: 623
Reputation: 70106
safe = robust, good practice, BUG free, etc.
It is always "safe" insofar as it is robust on your end and has well-defined behavior (succeeds or fails), and there is little room for bugs, but in the case of TerminateProcess
it is nevertheless very much not advisable.
Posting a quit message to the main thread or another process' thread may or may not work immediately, or at all. It is, however, a graceful, clean way of asking a process to quit. It may or may not honor that request, and it may do so while preserving all user data and leaving everything in a well-defined state (on its end).
Calling TerminateProcess
will, assuming that it does not fail, simply cause the operating system to no longer assign any CPU time to that process (including your own, if that is the one you terminate), close all handles held by the process, mark private memory pages as unused, etc.
It will work the same way when it's your own process that gets terminated, and since you anticipate this when calling TerminateProcess
, no bad things will happen, at least on the calling thread's end.
Terminating a process means among other things that the process will not write any data that is held e.g. in the C standard library's or similar buffers (from any thread within the process, anything that is "user land" simply goes poof).
The program does not have a chance to bring any of its half-complete data files that it may have into a consistent state. Another (unknown to you) process that you kill in this way might terminate in the middle of a set of registry changes which will now be non-consistent.
Also, you have no strict guarantee that data to be written and still buffered by Windows will actually be written (practically this is guaranteed by the way it's implemented, but you have no formal guarantee, also copies to the system buffers might be partial, and the program can no longer react to failure conditions).
Upvotes: 1
Reputation: 17415
Calling TerminateProcess()
will terminate the target process (or fail, e.g. without sufficient permissions). The fact that it is called from a different thread than the initial thread is irrelevant. If it kills the same process it is called from, then that process is killed, there is no special case for that.
Upvotes: 2