Solved

Threadsafe code

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 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