Solved

Casting From Thread Safe Class

Posted on 1999-01-27
1
219 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
1 Comment
 
LVL 1

Accepted Solution

by:
gvg earned 100 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 57
Pressing cancel button with C# UI Automation 3 39
Replacement selected text 2 47
MaxSpan challenge 9 65
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: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now