Solved

Threadsafe code

Posted on 2003-11-13
2
254 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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