Solved

Threadsafe code

Posted on 2003-11-13
2
261 Views
Last Modified: 2010-04-01
I got tired of constantly making mutexes and having to manually wait for them and release them and create them.

So I thought good time for an OO solution and set about to build a SynchronizedData template class that will keep any type of object from being accessed simultaneously.

I think the basic design is right, but I'm not sure I did the Lock() or Unlock() method correctly. Does it look good to you?

template<class T>
class SynchronizedData
{
private:
      T mData;
      HANDLE mMutex;

public:
      SynchronizedData() { mMutex = CreateMutex(NULL,FALSE,NULL); }

      T GetData();
      void SetData(T val);

      void Lock();
      void Unlock();
};

template<class T>
inline void SynchronizedData<T>::Lock()
{
      WaitForSingleObject(mMutex,INFINITE); //wait for object and then lock it
      mMutex = CreateMutex(NULL,TRUE,NULL); //locked
}

template<class T>
inline void SynchronizedData<T>::Unlock()
{
      ReleaseMutex(mMutex);
}

template<class T>
void SynchronizedData<T>::SetData(T val)
{
      this->Lock();
      mData = val;
      this->Unlock();
}

template<class T>
T SynchronizedData<T>::GetData()
{
      this->Lock();
      T retVal = mData;
      this->Unlock();
      return retVal;
}
0
Comment
Question by:Sandra-24
2 Comments
 
LVL 4

Accepted Solution

by:
YuriPutivsky earned 500 total points
ID: 9744248
Take a look at the professional multithreaded implementation - can get a lot of ideas!
http://www.terimber.com/text/opensrc/thread.html
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 9744614
Excellent classes. I borrowed the whole lot for my code:)

I suspect I will also end up using their thread class in my code as well.

Thanks for the link!

-Sandra
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Prototype for MessageDlg in Embacadero XE 2 2 42
Beginner to Unreal Engine 4 5 94
VS2015 Redefinition errors 4 67
Android development question 2 52
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

808 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