Thread: Native WinAPI thread "Self destruct"
 |
This question is answered.
Helpful answers available: 2.
Correct answers available: 1.
|
|
Replies:
2
-
Last Post:
Dec 13, 2017 3:19 PM
Last Post By: Goran Ekstrom
|
|
|
Posts:
149
Registered:
1/10/04
|
|
Hi,
I have a native (non-vcl) thread that I want to "self destruct" after completion, i.e. I do not want to monitor the thread's state and then destroy it from outside.
So, I have placed the CloseHandle just before the thread function exit like below:
DWORD WINAPI ThreadFunction(LPVOID lpParam)
{
while(!ThreadExit)
{
}
CloseHandle(ThreadHandle);
return 0;
}
Is there any danger in doing this or a "better" way?
Regards
Goran
|
|
|
Posts:
9,447
Registered:
12/23/01
|
|
Goran Ekstrom wrote:
I have a native (non-vcl) thread that I want to "self destruct" after
completion, i.e. I do not want to monitor the thread's state and then
destroy it from outside.
There is nothing to destroy, really. The thread object is destroyed
automatically by the kernel after the thread has terminated and all
handles to it have been closed. If you don't need to keep track of the
thread, then simply close the thread handle immediately after
CreateThread() exits (the thread will keep running until complete).
--
Remy Lebeau (TeamB)
|
|
|
|
Posts:
149
Registered:
1/10/04
|
|
... The thread object is destroyed
automatically by the kernel after the thread has terminated and all
handles to it have been closed.
... simply close the thread handle immediately after
CreateThread() exits (the thread will keep running until complete).
Thanks Remy, worked fine.
|
|
|
|
Legend
|
|
Helpful Answer
(5 pts)
|
|
Correct Answer
(10 pts)
|
|
Connect with Us