使用ExitThread关闭线程-C [英] Closing thread using ExitThread - C

查看:576
本文介绍了使用ExitThread关闭线程-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,该程序创建一个线程,循环20次,然后进行调用以关闭自身并执行必要的清除操作.

I have a simple program that creates a thread, loops twenty times and then makes a call to close itself and perform the necessary cleanup.

当我调试程序时,它到达ExitThread();方法并暂停,忽略printf();我已经设置好了,以通知我它已关闭.

When I debug the program it reaches the ExitThread(); method and pauses, ignoring the printf(); I have set up after it to signal to me it's closed.

这是正常现象还是我忘记做某事?我是使用C进行线程处理的新手.

Is this normal or am I forgetting to do something? I'm new to threading using C.

Main()

void main()
{
    Time t;
    int i = 0;

    StartTimer();

    for(i = 0; i < 20; i++)
    {
        t = GetTime();
        printf("%d.%.3d\n", t.seconds, t.milliseconds);
        Sleep(100);
    }
    StopTimer();
}

线程创建

void StartTimer()
{
    DWORD threadId;
seconds = 0;
milliseconds = 0;

// Create child thread
hThread = CreateThread(
    NULL,       // lpThreadAttributes (default)
    0,          // dwStackSize (default)
    ThreadFunc, // lpStartAddress
    NULL,       // lpParameter
    0,          // dwCreationFlags
    &threadId   // lpThreadId (returned by function)
    );

// Check child thread was created successfully
if(hThread == NULL)
{
    printf("Error creating thread\n");
}
}

线程关闭

void StopTimer()
{
    DWORD exitCode;

    if(GetExitCodeThread(hThread,&exitCode) != 0)
    {
        ExitThread(exitCode);   
        printf("Thread closed");

        if(CloseHandle(hThread))
        {
            printf("Handle closed");
        }
    }
}

我已经意识到ExitThread()会停止正在执行的线程,因此我尝试使用其他方法来停止计时器,但是我仍然得到相同的结果.

I've realised that ExitThread() stops the executing thread, therefore I have attempted to use a different method to stop the timer but i'm still left with the same result.

void StopTimer()
{    
    WaitForSingleObject(hThread,INFINITE);
}

解决方案:

程序在WaitForSingleObject()调用处停止并且无论进行多长时间都没有进展的原因是因为它正在等待的操作具有无限循环.

The reason the program was stopping at the WaitForSingleObject() call and not progressing no matter how long it took was because the operation it was waiting on had an infinite loop.

这将等待无限循环完成,而不是最好的做法.将WaitForSingleObject()的延迟更改为更合理的延迟(例如100)可以使程序正常运行.

It would be waiting for an infinite loop to finish, not the best thing to do. Changing the delay of WaitForSingleObject() to something more reasonable, such as 100 allowed the program to run normally.

感谢所有输入.

推荐答案

是的,这是正常的",通过调用ExitThread,您可以告诉Windows您已经完成了该调用线程,因此其余代码是不会被打来的.有点像调用exit(0).

Yes this is "normal", by calling ExitThread, you're telling to windows you're done with that calling thread, so the rest of the code isn't going to get called. It's a bit like calling exit(0).

我个人不会这样使用它.其余代码都可以,您已经正确清理了线程,只需正常退出线程功能即可.

Personally I wouldn't use it like this. The rest of the code is OK, you're properly cleaning up the thread, just exit your thread function normally.

这篇关于使用ExitThread关闭线程-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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