如何杀死这个线程,你能解释一下创建和退出线程吗?我正在研究VC ++,Windows线程 [英] How to kill this thread, Can you explain create and exit thread. I am working on VC++,Windows thread

查看:133
本文介绍了如何杀死这个线程,你能解释一下创建和退出线程吗?我正在研究VC ++,Windows线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

ExitThread是首选用C代码退出线程的方法。但是,在C ++代码中,在调用任何析构函数或执行任何其他自动清理之前退出该线程。因此,在C ++代码中,你应该从你的线程函数返回。



来自msdn:

TerminateThread是危险的功能,只应在最极端的情况下使用。只有当您确切知道目标线程正在执行的操作时,才应调用TerminateThread,并且您可以控制目标线程在终止时可能正在运行的所有代码。例如,TerminateThread可能会导致以下问题:

•如果目标线程拥有临界区,则不会释放临界区。

•如果目标线程从堆中分配内存,堆锁不会被释放。

•如果目标线程在终止时执行某些kernel32调用,则线程进程的kernel32状态可能不一致。 br />
•如果目标线程正在操纵共享DLL的全局状态,则DLL的状态可能会被破坏,从而影响DLL的其他用户。




研究这些线程函​​数的实现。如果线程必须执行一次性操作,您可能只是通过线程函数并等待它的句柄完成。

如果线程必须在循环中执行重复的功能或阻塞等待事件你需要通过其他一些机制发出信号通知。





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



使用CreateThread()API创建线程 [ ^ ]
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[^]


您可以让启动的线程例程包含导致的代码线程例程e返回。这将自动调用ExitThread。您的代码可以使用WaitForSingleObject函数在线程句柄上等待,并且在调用EndThread时,WaitForSingleObject将取消阻止。之后,通过调用CloseHandle函数释放CreateThread函数返回的线程句柄,并将线程句柄作为单个参数。



顺便说一句,我不会使用CreateThread创建一个Windows线程,虽然它在某些情况下可以正常工作。我建议使用_beingthreadex函数,它与_endthreadex函数配对,当你的线程例程退出时,它也会自动调用。



有三个WIN32函数可以创建一个线程,只有其中一个是完全正确的!



如你所知,CreateThread函数与ExitThread配对。遗憾的是,这些函数的实现无法释放C运行时库内存,因此如果在内部分配内存的线程例程中进行C运行时库调用,则当线程例程退出时,C运行时内存不会被释放,直到进程退出。



由于这个错误,Microsoft创建了_beginthread和相关的_endthread函数。不幸的是,又犯了一个错误。 _endthread关闭了线程句柄,这使得在调用_endthread后使用WaitForSingleObject无法在线程句柄上等待。等待一个封闭的线程句柄会导致访问冲突。



因此,微软写了一个完全正确的实现,即_beingthreadex和_endthreadex。



因此,早期的函数可以在有限的上下文中使用而不会引起问题,但最新的线程函数_beingthreadex和_endthreadex始终可以正常工作。同样,您不需要显式调用_endthreadex,它会在您的线程例程退出时调用。
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.


您可以使用TerminateThread并将其传递给线程句柄但不建议使用它。 />
你应该理想地实现一些机制来通知线程它必须停止并等待线程优雅地终止。
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.


这篇关于如何杀死这个线程,你能解释一下创建和退出线程吗?我正在研究VC ++,Windows线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆