Solved

Threadsafe code

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

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
Handle Exceptions during instantiation 28 428
computer science syllabus 3 100
VS2015 Redefinition errors 4 87
C++ Code Issue 4 37
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

735 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