Thread-safe class instances

Posted on 2003-03-04
Medium Priority
Last Modified: 2010-04-01
How do I make class methods thread-safe for each particular class instance, without threads for one instance interfering with the threads for the other instances?


// I have a few instances of MyClass
MyClass A, B, C;

// And I have a thread function, using MyClass instance
ThreadFunction(MyClass * pMyClass)

Then I am launching a lot of ThreadFunction threads, passing to each a pointer to the MyClass instance (pointer to A, B or C). Each instance (like A) may be passed to several threads; the total number of instances is not limited. I need all threads using instance A be thread-safe with respect to each other; same for all threads using instance B, etc. However, all A-threads should not block any of B-threads, etc. For example, A, B, C,… may be different files, and ThreadFunction(…) may be a file-writing function. In this case, all threads writing to the same file should be safe, but they should not interfere with the threads writing to the other files.

My app runs under Windows. If possible, I would like to avoid creating a critical section for each class instance. I also need the most time-efficient solution. I am new to thread-safe programming, so please be specific.

Question by:olegsp
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
  • 2

Expert Comment

ID: 8069668
You have to lock/unlock areas in which the threads might conflict. A simple example would be writing to a certain file (if one thread has it open the other would fail). To make it thread safe you would need to either use a CCriticalSection object or something better like CSingleLock or CMutex. You would then lock the section where problems would happen and unlock when done. You just have to be carefull where you put the lock so that threads aren't waiting around needlessly. There's not much other way to do it.
LVL 48

Expert Comment

ID: 8069825
Actually, critical section for each class instance is exactly what you need. Any class function which should be thread-safe locks this critical section on the start and releases it before exit.

Author Comment

ID: 8071853
So, if I make a critical section a member of my class, locking it will lock only its own class instance? And locking instance A will not lock instance B?

It's not like I cannot use critical sections in my class, it is more a design goal. I would really like to have the locking implemented outside the MyClass, if possible.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 48

Accepted Solution

AlexFM earned 400 total points
ID: 8072080
The best OO design desision is writing thread-safe classes. Thread safety is incapsulated in class itself and caller just call class class functions.

Example of thread-safe class:

class CSample
void SampleFunction();

    CCriticalSection m_CriticalSection;

void CSample::SampleFunction()
    // do something

The best place to protect class members from accessing from multimple threads is class functions. Every class instance has it's own critical section which protects this instance members. This is an optimal solution, just use it.

LVL 48

Expert Comment

ID: 8072119
Addintinal information:
suppose you call pA->SampleFunction() from class B functions in different threads. Class A is thread-safety, this means, these two calls will not intercept each other, the will be executed one after other.
Obviously, you shouldn't call functions MessageBox when critical section is locked.

Author Comment

ID: 8073103
OK, I agree. Is it possible then to disable thread-locking in some instances, and have it enabled in the others? 80% of my CSample objects are processed only on the main thread, and locking/unlocking them is not necessary (plus, it would introduce some performance overhead, I suppose?). I would like to have something like


so that calling it will set my object in the instance-thread-safe state.
Processing 80% of objects on a single thread was the reason why I did not want to do any thread locking inside the object. However, if it needs to be done, I need to have at least some way to ignore critical sections for the objects which remain on a single thread.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

762 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