Solved

Casting From Thread Safe Class

Posted on 1999-01-27
1
225 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 100 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

728 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