Solved

Threads and MFC classes

Posted on 1998-03-18
3
311 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
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…

624 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