识别线程 [英] identifying the threads

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

问题描述

有人可以告诉我,如何在编码中识别出哪个是工作线程,哪个是用户线程.

Can anybody tell me that how to identify that which is worker thread and which is user thread in coding

推荐答案

线程未分类为"worker",用户"线程等.线程是线程.但是,如果需要确定在运行时哪个线程正在执行某些代码,则可以使用Windows API GetCurrentThread,它为您提供了线程句柄:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/ms683182%28v=vs.85%29.aspx [
Threads are not classified into "worker", "user" threads, or the like. Threads are threads. However, if you need to identify what thread is executing some code during run time, you can use the Windows API GetCurrentThread, which gives you a thread handle:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683182%28v=vs.85%29.aspx[^].

Just remember, that a value of a thread handle makes sense only in same the process, where this thread is running. The function takes the handle of the thread of the calling code. For example, you can get thread handles in the very beginning of the thread body function, when you certainly know what thread is running, store those values somewhere and later compare with the handles taken during execution of some function in question.

—SA


从代码中,您可以识别出哪个是工作线程,哪个是UI线程.
UI线程类始终从CWinThread派生.您可以检入继承类CWinThread的代码.
辅助线程功能可能如下所示
From the code you can identify which is worker thread and which is UI thread.
The UI thread class is always derived from CWinThread. You can check in the code who is inherting the class CWinThread.
The worker thread function may look like below
UINT ThreadFunc (LPVOID pParam)
{
    :
    :
    return 0;
}


和启动该线程的代码可能会像这样


and code starting this thread may like this

CWinThread* pThread = AfxBeginThread (ThreadFunc, &threadInfo);



UI线程类声明可能如下所示



A UI thread class declartion may look like below

class CUIThread : public CWinThread
{
    DECLARE_DYNCREATE (CUIThread)

public:
    virtual BOOL InitInstance ();
};


以及如下创建UI线程的代码


and the code creating the UI thread as below

CWinThread* pThread = AfxBeginThread (RUNTIME_CLASS (CUIThread));


这只会帮助您进行识别.可能会更进一步,您应该对两者之间的区别有很好的了解.为此,您可以参考任何好的教程


This just helps you in identfying. May be to proceed further, you should have good understnding on the difference between both. For that you can refer any good tutorial


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

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