Solved

Threads and MFC classes

Posted on 1998-03-18
3
299 Views
Last Modified: 2013-12-14
I've actually got both a general question, and a specific problem.

The general question is, when is it OK to use the same MFC object in two threads? The book I am reading (Kruglinsky) is somewhat vague and says that this can't ever be done safely, "except simple classes like CString". What constitutes a "simple class"? Also, if I ensure that an object is only accessed by one thread at a time (using critical sections, for instance), will it then be safe to use the same object in two threads?
0
Comment
Question by:Slarti
3 Comments
 
LVL 5

Expert Comment

by:yonat
Comment Utility
I am not sure about CTypedObList, but I have used STL containers across threads (with appropriate locks) and it works fine. The STL version I usually use even does the locking automatically.

You may consider having the processing do all the queue management, without the recorder thread being aware of it. Ie, the recorder thread allocates a new buffer and puts data into it, the passes control of the buffer to the processing thread. The processing thread is responsible of maintaining the queue of buffers and deallocating buffers after they are no longer needed. Just a thought.
0
 
LVL 11

Accepted Solution

by:
alexo earned 70 total points
Comment Utility
Slarti, I have used MFC containers in multithreaded programs.
As long as you use a critical section to lock the object, you're fine.

I suggest you make your ouw object which contains a CTypedObList (or privately inherits from it) and provide operations that will lock it, pass to the CTypedObList and unlock.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
What may be confussing to you slarti, is that when they say the containers "are not thread safe"  That means they are not automatically thread safe, and you must take extra steps to make them thread safe.  Using mutexes, cirtical sections, semaphores, etc will make them thread safe.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now