如何停止后台线程 [英] How to stop background thread

查看:88
本文介绍了如何停止后台线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有4个后台线程正在运行.我想通过发信号通知所有线程来关闭应用程序.我可以使用WaitForMultipleObjects等待所有后台线程,但是如何向所有正在运行的线程发出信号.

关于

Hi All,

I have 4 background threading running. I want to close application by signaling all the thread. I can wait for all the background thread using WaitForMultipleObjects, but how to signal to all running thread.

Regards

推荐答案

通常,您在线程过程中建立了某种类型的线程间通信.
例如,如果您使用的是PostThreadMessage():

Usually, you set up some kind of inter-thread communication in your thread procedure.
For instance, if you''re using PostThreadMessage():

DWORD WINAPI ThreadProc(LPVOID pX)
{
    // Cast passed pointer to what it really is.
    if (pX != NULL)
    {
        X *realPointer = reinterpret_cast<x> pX;
        // Initialize COM
        // Generate message pump
        ::PeekMessage(whatever);
        // Connect to database
        while (!done)
        {
            ::GetMessage(....)
            switch(msg.uMsg)
            {
                case WM_COMMAND: // Cast LPARAM to pointer to a Job, execute that job.
                    realPointer->ExecuteCommand(msg.lParam);
                    break;
                case WM_QUIT:// This is your cue to exith the thread.
                    done = true;
                    break;
 
            }
        } // while
        SetEvent(realPointer->reallyDone);
    } // if, scope for casted pointer.
    return 0;
}
</x>


无论如何,服务器线程和客户端线程必须能够通信. Google的线程设计模式".

如果您使用的是拍摄后忘记"线程,则可以猛烈地杀死它们,但这是一件坏事.根据您的实际情况,最佳选择可能是隐藏主窗口,然后等待所有线程完成执行.

希望这会有所帮助,
Pablo.


In any case, the server thread and the client thread must be able to communicate. Google for ''thread design patterns''.

If you''re using ''shoot and forget'' threads, you can kill them violently, but this is a bad thing. Your best option, depending on your actual circumstances, may be to hide your main window, and just wait for all threads to complete execution.

Hope this helps,
Pablo.


通常,您会传达希望通过某些全局变量或通常分配给控制线程的每个线程数据块中的变量来停止的愿望.然后需要在线程的主循环中测试该变量.当看到该变量设置为false时,线程将在下一次迭代和清理之后停止.
Usually you would communicate the wish to stop by some global variable or a variable in the per-thread data block that you typically allocate to control the thread. That variable needs then to be tested in the main loop of your threads. When seeing that variable is set to false, the thread would then stop after the next iteration and after cleaning up.


根据我的经验,通常最好让线程自己优雅地退出.如果出现问题,将主线程设置为false的公共变量如何处理.第二个线程可以定期检查此变量,如果已将其设置为false,则知道应该清除并返回.

我认为您可以更优雅地使用条件变量,但是正确使用NSLock的方法应该可以.
In my experience, its usually best to let a thread exit gracefully by itself. How about having some common variable that the main thread could set to false if things go awry. The second thread could periodically check this variable and if it has been set to false, it knows it should clean up and return.

I think you could use condition variables in a more elegant fashion, but his approach with the proper use of NSLock should work.


这篇关于如何停止后台线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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