Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Threads and MFC classes

Posted on 1998-03-18
3
Medium Priority
?
312 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 210 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

Industry Leaders: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

730 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