Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 313
  • Last Modified:

Threads and MFC classes

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
Slarti
Asked:
Slarti
1 Solution
 
yonatCommented:
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
 
alexoCommented:
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
 
nietodCommented:
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now