Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Casting From Thread Safe Class

Posted on 1999-01-27
1
Medium Priority
?
229 Views
Last Modified: 2013-11-20
I've got a class for data which I want to use in both a base CWinThread and also a worker thread. I've made a child class of the data class adding only a CMutex and Get and Set functions. Now bearing in mind that I must use CSingleLock or CMultiLock to protect my objects when the worker thread is running, what ought to happen when I cast from the thread safe data class to the thread unsafe data class?
0
Comment
Question by:gavinpat
[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
1 Comment
 
LVL 1

Accepted Solution

by:
gvg earned 300 total points
ID: 1328385
If you have dataclass as such

Class A
{
public:
  int  mNumber
}

class B : public A
{
  CMutex  mLock;
  int GetNumber( void );
  void SetNumber( int number );
}

int B:GetNumber()
{
  CSingleLock lock( mLock );
  return mNumber;
}

void B:SetNumber( int number )
{
  CSingleLock lock( mLock );
  mNumber = number;
}

Now you must access the members through the Get & Set functions to make them thread safe no matter if you are using class A or class B.  This is because a direct access to the member does not lock the threadlock. You can however not access the Get & Set functions in class A since they aren't defined.  What you should do is this


class A
{
protected:
  int mNumber;
public:
  virtual int GetNumber( void );
  virtual void SetNumber( int number );
}

class B : public A
{
public:
  CMutex mLock;
  virtual int GetNumber( void );
  virtual void SetNumber( void );
}

Now you can impliment the Get & Set members for both class A and class B.  If you create a instance of class B and cast it to A you will be able to access the member throught the Get & Set function and the virtual keyword will garanty that if the class was created as class B you will use its functions after you cast it to class A so it will be thread safe.

I also made the data member "mNumber" as protected so it can not be accessed directly by other classes.

Tip:
If you program is running entierly in a single process you should use CCriticalSection instead of CMutex.  There is not diffrance between this two classes except that the Mutex can work across process boundaries and is therefor much slower than CCriticalSection.

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

636 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