?
Solved

Casting From Thread Safe Class

Posted on 1999-01-27
1
Medium Priority
?
228 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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