Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Thread-safe class instances

Posted on 2003-03-04
6
Medium Priority
?
343 Views
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?

Example:

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

// And I have a thread function, using MyClass instance
ThreadFunction(MyClass * pMyClass)
{
….
   pMyClass->Func1();
….
   pMyClass->Func2();
….
}

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.

0
Comment
Question by:olegsp
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:MatrixDud
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.
0
 
LVL 48

Expert Comment

by:AlexFM
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.
0
 
LVL 1

Author Comment

by:olegsp
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 48

Accepted Solution

by:
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
{
public:
void SampleFunction();

private:
    CCriticalSection m_CriticalSection;
};

void CSample::SampleFunction()
{
    m_CriticalSection.Lock();
    // do something
    m_CriticalSection.Unlock();
}

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.

   
0
 
LVL 48

Expert Comment

by:AlexFM
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.
0
 
LVL 1

Author Comment

by:olegsp
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

pA->MakeThreadSafe(),

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.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 how to clear a vector as well as how to detect empty vectors in C++.

578 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