Solved

Threads and MFC classes

Posted on 1998-03-18
3
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1183637
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
ID: 1183638
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
ID: 1183639
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 93
Better understanding on C++ Class serialization and formats 9 105
C++ Code Issue 4 48
netbeans can not run self signed app 8 37
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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

738 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