有关在MFC程序中使用线程的一般问题 [英] A general question on the use of threads in an MFC program

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

问题描述

我当前正在尝试修改程序,似乎在一个成员函数中调用了创建线程.程序的这一部分是事件处理程序的开始按钮,该按钮指示程序检索信息并最终用图形更新窗口.这是我指的代码.

I am currently trying to modify a program and it seems that within one of the member functions there is call to create thread. This part of the program is the event handler to a start button that directs the program to retrieve information and eventually update a window with a graph. Here is the code I am referring to.

void CDlg_SpectrumAnalyzer::OnSpectrumStart()
{
    CString txt;
    CComboBox *the_Box = (CComboBox*)GetDlgItem(IDC_SPEC_CHAN);

    int sel = the_Box->GetCurSel();
    m_AcqChan = the_Box->GetItemData(sel);

    CButton *btn = (CButton*)GetDlgItem(IDC_SPECTRUM_START);
    btn->GetWindowText(txt);

    if(txt == "Start" && UpdateData(true))
    {
        // reserve instrument
        if(!m_inst->StartOperation())
        {
            MessageBox("The Instrument is Currently in use by another function");
            return;
        }


        if(! CreateData(m_MaxFreq, m_Resolution))
            return;
        EnableItems(false);
        m_Continue = true;
        m_datacount = 0;
        GetDlgItem(IDC_SPECTRUM_START)->SetWindowText("Stop");
        m_Thread = AfxBeginThread(Acq_Data,this,THREAD_PRIORITY_HIGHEST);
        return;
    }
    else if(txt == "Stop")
    {
        m_Continue = false;
        EnableItems(true);

        m_inst->HaltOperation();
        GetDlgItem(IDC_SPECTRUM_START)->SetWindowText("Start");
        return;
    }
}



具体来说,我指的是这一行代码.



Specifically, I am referring to this line of code.

m_Thread = AfxBeginThread(Acq_Data,this,THREAD_PRIORITY_HIGHEST);



我是线程技术的新手,已经阅读了一些文章,但我仍然感到困惑.在这里使用线程的好处是什么?该行的m_Thread部分是否必要,因为我在类实现的其余部分中看不到它(该代码未发布)?另外,只要包含正确的标头,我就可以在类中任何地方创建,销毁消息或将消息发送到线程吗?



I am new to threading and have been reading through some articles but I am still confused. What is the advantage of using a thread here? Is the m_Thread part of the line necessary since I do not see it in the rest of the class implementation (that code is not posted)? Also, as long as the correct headers are included can I create, destroy, or send a message to a thread anywhere in the class?

推荐答案

线程是一种解决方法创建一个可单独调度的实体",这是一个代码块,它自己运行,并与创建它的代码分开.它们非常适合利用多核和/或芯片中的超线程,以便您可以最大程度地提高在特定时间单位内完成的工作量.线程的优点是它们共享相同的内存上下文,但堆栈分开.当然,这种优势可能会被滥用,并且需要一些思考和使用同步原语(Mutex,Semaphores等)来控制对共享内存/变量/结构/对象的访问.

在Windows编程中,"UI事件"很常见,例如按操作按钮"创建工作线程",该线程在创建"代码继续监视UI的同时进行所有艰苦的工作.事件(可能是停止"按钮或导出"按钮)与工作线程完成其工作(在这种情况下为数据获取)无关.因此,在此代码中创建线程调用的放置是很合逻辑的,它位于"Spectrum Start"(频谱开始)按钮上(有根据的猜测).

除了示例/案例之外,还有很多创建线程的原因.我建议您使用Google/Bing"Worker Thread"或查看一些示例.

线程没有与创建者通信的标准"方式(有时称为父线程"),因此发送消息"可能是最佳方法,也可能不是最佳方法.其他方式包括设置事件或状态变量.整个应用程序的设计将确定最佳方法.由于这是您要修改的代码,因此您可能需要查看如何播放结束游戏",即,我们看到如何开始数据采集",但是此应用程序如何知道何时停止?弄清楚这一点将向您显示主/GUI线程与数据获取线程之间的通信.

最后,m_Thread对象是您如何控制创建的线程的方法.对于长期运行(或永不终止)的工作人员,您可以忽略它.但是,如果您要监视它是否完成或以编程方式停止它,则它确实很有用.同样,请查看该特定应用程序,以了解它们如何使用(或不使用)线程.
Threads are a way of creating a "separately schedulable entitity", a block of code that runs on its own and separate from the code that creates it. They are great for taking advantage of multiple cores and/or hyperthreading in chips so that you can maximize the amount of work done in a particular unit of time. The advantage of threads is that they share the same memory context but separate stacks. This advantage can be misused of course and requires some thought and use of synchronization primitives (Mutex, Semaphores, etc) to control access to shared memory / variables / structures / objects.

In Windows programming, it''s quite common to have a "UI Event", like pushing an "action button" create a "worker thread" that goes of and does all the hard work while the "creating" code continues to monitor UI Events (maybe a "stop" button or an "export" button) independently of the worker thread doing its job (in this case, data acquisition). So, the placement of the create thread call in this code is quite logical, it''s on a "Spectrum Start" button (educated guess).

There are lots of reasons to create threads beyond your example / case. I suggest you Google / Bing "Worker Thread" or and look at some examples.

Threads do not have a "standard" way of communicating to the creator (sometimes call "the parent thread") so "Send Message" may or may not be the best way. Other ways include setting events or state variables. The design of the total application will determine the best method. Since this is code you are modifying, you might want to look at how the "end game" is played out, that is, we see how to "start data acquisition" but how does this application know when to stop? Figuring that out will show you the communication between the main / GUI thread and the data acquisition thread.

Lastly, the m_Thread object is how you can control the created thread. For long running (or never terminating) workers, you can ignore it. However, it is real useful if you want to watch for it to complete or programatically stop it. Again, look to this particular application to see how they use (or don''t use) the thread.


这篇关于有关在MFC程序中使用线程的一般问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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