Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
C++
HANDLE ghThread;
DWORD   dwThreadID;

 ghThread = CreateThread(
            NULL,                                                                  // default security
            0,                                                                        // default stack size
            (LPTHREAD_START_ROUTINE)ThreadProc,         // name of the thread function
            this,                                                                  //  thread parameters
            0,                                                                     // default startup flags
            &dwThreadID);


How to kill this thread, Can you explain create and exit thread.

Please write the code snippet for exiting thread.
Posted
Updated 14-Oct-13 19:02pm
v2

ExitThread is the preferred method of exiting a thread in C code. However, in C++ code, the thread is exited before any destructors can be called or any other automatic cleanup can be performed. Therefore, in C++ code, you should return from your thread function.

From msdn:
TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:
•If the target thread owns a critical section, the critical section will not be released.
•If the target thread is allocating memory from the heap, the heap lock will not be released.
•If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
•If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.


Study these implementations of thread functions. If the thread has to perform a one off action you may just fall through the thread function and wait on it's handle for completion.
If the thread has to perform repeated functions in a loop or block waiting for events you need to signal the thread to exit by some other mechanism.


http://www.bogotobogo.com/cplusplus/multithreading_win32A.php[^]

Creating Threads using the CreateThread() API[^]
 
Share this answer
 
v3
Comments
pasztorpisti 15-Oct-13 3:40am    
+5, this is very important in case of C++. I've read that on some older windows versions killing a thread from outside with TerminateThread() caused leaks even on OS level.
[no name] 15-Oct-13 6:34am    
Thanks. It is always posible to exit gracefully. I will update my answer.
You can have the launched thread routine contain code that causes the thread routine to return. This will automatically cause ExitThread to be called. Your code can wait on the thread handle using the WaitForSingleObject function and when EndThread is called, WaitForSingleObject will unblock. After that, free the thread handle returned by the CreateThread function by calling the CloseHandle function with the thread handle as the single argument.

By the way, I wouldn't use CreateThread to create a Windows thread, although it will work okay in some cases. I recommend using the _beingthreadex function, which is paired with the _endthreadex function, which is also callled automatically when your thread routine exits.

There are three WIN32 functions to create a thread, and only one of them is completely correct!

The CreateThread function is paired with ExitThread, as you noted. Unfortunately, the implementation of these functions fails to free C-runtime library memory, so if you make a C-runtime library call in your thread routine that allocates memory internally, when the thread routine exits the C-runtime memory is not freed until the process exits.

Because of this bug, Microsoft created _beginthread and the associated _endthread function. Unfortunately, another mistake was made. _endthread closed the thread handle, which made is impossible to wait on a thread handle using WaitForSingleObject after _endthread had been called. Waiting on a closed thread handle results in an access violation.

So Microsoft wrote a totally correct implementation, which is _beingthreadex and _endthreadex.

So, the earlier functions can be used in limited contexts without causing issues, but the most recent thread functions, _beingthreadex and _endthreadex always work properly. Again, you don't need to call _endthreadex explicitly, it is called when your thread routine exits.
 
Share this answer
 
v2
Comments
[no name] 15-Oct-13 22:00pm    
Excellent summary.
You can use TerminateThread and pass it the thread handle but it is not recommended.
You should ideally implement some mechanism to signal the thread that it has to stop and wait for the thread to terminate graciously.
 
Share this answer
 
Comments
Albert Holguin 15-Oct-13 14:06pm    
+5... ideal case, you "ask" your thread to terminate (or alternatively, you "wait" for it to be done).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900