Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Native WinAPI thread "Self destruct"


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Dec 13, 2017 3:19 PM Last Post By: Goran Ekstrom Threads: [ Previous | Next ]
Goran Ekstrom

Posts: 137
Registered: 1/10/04
Native WinAPI thread "Self destruct"  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 13, 2017 12:50 PM
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
Remy Lebeau (Te...


Posts: 9,064
Registered: 12/23/01
Re: Native WinAPI thread "Self destruct"  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 13, 2017 2:19 PM   in response to: Goran Ekstrom in response to: Goran Ekstrom
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)
Goran Ekstrom

Posts: 137
Registered: 1/10/04
Re: Native WinAPI thread "Self destruct"  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 13, 2017 3:19 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
... 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)

Server Response from: ETNAJIVE02