有两个自定义函数A和B,当B执行时,A要马上退出,怎么做呢?

解决方案 »

  1.   

    HANDLE hEvent;
    A中
    hEvent=CreateEvent(NULL,FALSE,FALSE,NULL); 
    while(1)
    {
      if(WaitForSingleObject(hEvent,0)!=WAIT_TIMEOUT)
      {
        //退出
      }}B开始时
    SetEvent(hEvent);
      

  2.   

    kill 线程,和到处用goto没什么两样遇到问题不解决问题,而是急躁蛮干。code killer的作风
      

  3.   

    同步问题。
    同步的方法很多,建议看下《Windows核心编程》(第五版)
      

  4.   

    在B开始时,TerminateThread(threadA,0);就好了。TerminateThread
      函数的声明如下:
      BOOL TerminateThread(  HANDLE hThread,   DWORD dwExitCode);
      作用:
      终止一个线程
      
      参数说明:
      HANDLE htread:被终止的线程的句柄
      DWORD dwExitCode:退出代码
      返回值:
      函数执行成功则返回非零值,执行失败返回0。调用getlasterror获得返回的值有问题随时问我。
      

  5.   

    Res
    TerminateThread is used to cause a thread to exit. When this occurs, the target thread has no chance to execute any user-mode code. DLLs attached to the thread are not notified that the thread is terminating. The system frees the thread's initial stack.Windows Server 2003 and Windows XP/2000:  The target thread's initial stack is not freed, causing a resource leak.
    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.
    A thread cannot protect itself against TerminateThread, other than by controlling access to its handles. The thread handle returned by the CreateThread and CreateProcess functions has THREAD_TERMINATE access, so any caller holding one of these handles can terminate your thread.If the target thread is the last thread of a process when this function is called, the thread's process is also terminated.The state of the thread object becomes signaled, releasing any other threads that had been waiting for the thread to terminate. The thread's termination status changes from STILL_ACTIVE to the value of the dwExitCode parameter.Terminating a thread does not necessarily remove the thread object from the system. A thread object is deleted when the last thread handle is closed.
      

  6.   

    Res
    TerminateThread用于结束一个线程。同时,目标线程会没有机会执行任何用户模式代码. 和该线程有关的DLLs将不会得到线程退出的通知. 系统将负责释放线程的调用栈.Windows Server 2003 和 Windows XP/2000:  目标线程的调用栈将不会被系统释放,由此引发一个资源泄露.
    TerminateThread是一个危险的函数,应该仅当最极端的情况下才被用到. 你只能在确切地清楚目标线程所作所为的情况下调用TerminateThread, 并且这时目标线程代码处于你完全地控制着的情况下. 例如, TerminateThread可能导致以下问题:•如果目标线程拥有关键区,关键区将不会被释放.
    •如果目标线程分配了堆内存, 锁定的堆将不会被释放.
    •如果调用TerminateThread时目标线程正在执行特定的kernel32调用, 则线程所在的进程内的kernel32就会处于未知状态.
    •如果目标线程正在维护着共享dll的全局状态, 该DLL的状态就可能被破坏并影响到该DLL的其它宿主.
    线程无法保护自己拒绝TerminateThread结束自己, 担可以控制自己线程句柄的访问权限. 由CreateThread和CreateProcess反回的线程句柄拥有THREAD_TERMINATE访问权, 所以任何控制这句柄的调用者都可以强制结束线程.如果目标线程是进程内的最后一个线程, 线程所在的进程也会终止.线程对象的状态会被置信号,并让任何其它等待自己结束的线程释放等待状态.线程的终止状态码从STILL_ACTIVE变成参数dwExitCode给定的值
    强制结束一个线程不一定会将线程对象从系统删除掉,线程对象会在他的最后一个句柄被关闭时被(系统)删除。
      

  7.   

    滥用TerminateThread绝不是一个好习惯
      

  8.   


    goto 是个好东西,不要看不起这个语句啊
      

  9.   

    事实上我反对禁止goto的使用。具体可以看我的博客。
    但我很反对滥用goto、terminators等。
    另外这些goto和terminators会导致析构函数没有机会被执行或错误地被调用。