Solved

Casting From Thread Safe Class

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 59
ODBC Connection Logging, ADO.NET 6 73
notReplace  challenge 53 152
Execute multiple curl cmds with sleep and send output to file 10 121
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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