简体   繁体   English

MFC多线程:AfxBeginThread vs Boost.Thread?

[英]MFC multithreading: AfxBeginThread vs Boost.Thread?

I have an MFC application to which I want to incorporate multi-threading. 我有一个MFC应用程序,我想要包含多线程。 Originally I was thinking of using Boost.Thread because of the general ease of development with boost. 最初我正在考虑使用Boost.Thread,因为通常可以通过boost进行开发。 However, I'm wondering if there are any gotchas and if I should use AfxBeginThread to be safe in an MFC context? 但是,我想知道是否有任何陷阱,是否应该使用AfxBeginThread在MFC上下文中是安全的?

Just include the following header file and there should be no problems: 只需包含以下头文件,应该没有问题:

#include <boost/thread/win32/mfc_thread_init.hpp>
#include <boost/thread.hpp>

Try to read this article, in the section Concurrent Programming Improvements this is the best way do things on the Windows platform right now. 尝试阅读本文,在“ 并发编程改进 ”一节中,这是Windows平台上最好的方法。 Boost slows down your builds and contains a lot of extra stuff you don not need. Boost会减慢你的构建速度并包含许多你不需要的额外内容。

Also check the MSDN site on parallel programming for more details. 有关更多详细信息,请查看有关并行编程的MSDN站点。

One benefit of using boost::thread is that its interface resembles std::thread 's. 使用boost::thread一个好处是它的接口类似于std::thread There are, however, a few differences. 但是,有一些差异。 If you ultimately want to use std::thread , boost::thread will be a straightforward transition. 如果你最终想要使用std::threadboost::thread将是一个简单的过渡。

I found this Microsoft article: Multithreading: Programming Tips . 我发现了这篇微软文章: 多线程:编程技巧 It says: 它说:

Accessing MFC Objects from Non-MFC Threads 从非MFC线程访问MFC对象

If you have a multithreaded application that creates a thread in a way other than using a CWinThread object, you cannot access other MFC objects from that thread. 如果您的多线程应用程序以不同于使用CWinThread对象的方式创建线程,则无法从该线程访问其他MFC对象。 In other words, if you want to access any MFC object from a secondary thread, you must create that thread with one of the methods described in Multithreading: Creating User-Interface Threads or Multithreading: Creating Worker Threads. 换句话说,如果要从辅助线程访问任何MFC对象,则必须使用多线程:创建用户界面线程或多线程:创建工作线程中描述的方法之一创建该线程。 These methods are the only ones that allow the class library to initialize the internal variables necessary to handle multithreaded applications. 这些方法是唯一允许类库初始化处理多线程应用程序所必需的内部变量的方法。

I was intending to use the threading in a model view presenter context in order to multi-thread some long running presenter tasks. 我打算在模型视图演示者上下文中使用线程,以便多线程一些长时间运行的演示者任务。 As the presenter needs to communicate with the view I think that most likely qualifies as accessing MFC objects (at least indirectly). 由于演示者需要与视图进行通信,我认为很可能有资格访问MFC对象(至少间接地)。 Based on that I've decide to go for AfxBeginThread thread creation method for now. 基于此我现在决定采用AfxBeginThread线程创建方法。

At the time I write this, it is still unsafe because of this issue in Boost.Thread: 在我写这篇文章的时候,由于Boost.Thread中的这个问题,它仍然是不安全的:

http://boost.2283326.n4.nabble.com/Fwd-Thread-Solution-to-conflict-with-MFC-td3477977.html http://boost.2283326.n4.nabble.com/Fwd-Thread-Solution-to-conflict-with-MFC-td3477977.html

(short version that anybody trying this discovers soon enough: merely linking with Boost.Thread causes asserts in MFC initialization) (任何人试图发现这个的简短版本很快就会发现:仅与Boost.Thread链接导致MFC初始化中的断言)

If you have MFC project, please DON'T introduce boost to it. 如果你有MFC项目,请不要引入它。 Use MFC API wherever you can. 尽可能使用MFC API。 Since it has some stuff for threading support, you really don't need one extra library (especially boost) for that. 因为它有一些用于线程支持的东西,所以你真的不需要一个额外的库(特别是boost)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM