User
User

Reputation: 65961

MFC multithreading: AfxBeginThread vs Boost.Thread?

I have an MFC application to which I want to incorporate multi-threading. Originally I was thinking of using Boost.Thread because of the general ease of development with boost. However, I'm wondering if there are any gotchas and if I should use AfxBeginThread to be safe in an MFC context?

Upvotes: 1

Views: 4165

Answers (6)

Bojo
Bojo

Reputation: 11

Just include the following header file and there should be no problems:

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

Upvotes: 1

V&#225;clav Slav&#237;k
V&#225;clav Slav&#237;k

Reputation: 6660

At the time I write this, it is still unsafe because of this issue in Boost.Thread:

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)

Upvotes: 0

Daniel Mošmondor
Daniel Mošmondor

Reputation: 19956

If you have MFC project, please DON'T introduce boost to it. Use MFC API wherever you can. Since it has some stuff for threading support, you really don't need one extra library (especially boost) for that.

Upvotes: 0

User
User

Reputation: 65961

I found this Microsoft article: Multithreading: Programming Tips. It says:

Accessing MFC Objects from Non-MFC Threads

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. 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. 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). Based on that I've decide to go for AfxBeginThread thread creation method for now.

Upvotes: 3

justin
justin

Reputation: 104698

One benefit of using boost::thread is that its interface resembles std::thread's. There are, however, a few differences. If you ultimately want to use std::thread, boost::thread will be a straightforward transition.

Upvotes: 1

Damian
Damian

Reputation: 4631

Try to read this article, in the section Concurrent Programming Improvements this is the best way do things on the Windows platform right now. Boost slows down your builds and contains a lot of extra stuff you don not need.

Also check the MSDN site on parallel programming for more details.

Upvotes: 1

Related Questions