Click to See Complete Forum and Search --> : Regarding Synchronization Objects.
Fandu_Nagesh
April 18th, 2005, 10:25 AM
Hi
I am writing one multi-threaded application. I need to synchronize the threads for data sharing.
But I am not getting which is suitable to use. I mean Mutex/CriticalSection or Semaphore.
I will be grateful if you provide me the details advantage and disadvantages of these.
Regards,
Nagesh
Siddhartha
April 18th, 2005, 10:41 AM
I think the Thread mentioned below should give you some information you need + links to FAQs.
Threads in Visual C++ (http://www.codeguru.com/forum/showthread.php?t=334454)
A useful link from MSDN...
Synchronization (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/synchronization.asp) :thumb:
Fandu_Nagesh
April 20th, 2005, 04:48 AM
Hi,
Thanks for such informative article.
Regards,
Nagesh
Siddhartha
April 20th, 2005, 04:51 AM
Thanks for such informative article.
You are welcome.
Ciao,
Sid!
BOswalt
April 20th, 2005, 07:41 AM
If you are doing a multithreaded application and locks across processes aren't necessary, then you almost always want to use a critical section.
The best way to do this, in my opinion, is to use the raw Win32 objects themselves, like this:
Class member or persistent member variable:
CRITICALSECTION m_cs;
Constructor or Init routine:
InitializeCriticalSection(&m_cs);
Destructor or Cleanup routine
DeleteCriticalSection(&m_cs);
Then, use a separate class like this:
CMyLock::CMyLock(LPCRITICAL_SECTION pCs) {
m_pCs = pCs;
EnterCriticalSection(pCs);
}
CMyLock::~CMyLock() {
LeaveCriticalSection(m_pCs);
}
Then, when you have a section that needs to be thread safe, you should lock like this:
void CMyClass::Function() {
// This section needs a critical section
{
CMyLock lock(&m_cs);
// Do my thread safe stuff
}
}
In this way, C++ scope rules take care of the locking/unlocking for you, and you can reduce your deadlock probability.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.