Click here to Skip to main content
15,912,281 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: tracing internet protocol address.... Pin
sunit528-Dec-06 18:17
sunit528-Dec-06 18:17 
GeneralRe: tracing internet protocol address.... Pin
Out Of Reach28-Dec-06 19:12
Out Of Reach28-Dec-06 19:12 
AnswerRe: tracing internet protocol address.... Pin
Shouvik Das28-Dec-06 19:06
Shouvik Das28-Dec-06 19:06 
Questionrepost: command bar problem Pin
eantaru28-Dec-06 15:14
eantaru28-Dec-06 15:14 
QuestionSVG file viewer or classes Pin
D_code_writer28-Dec-06 14:14
D_code_writer28-Dec-06 14:14 
QuestionWMI Win32_DiskDrivePhysicalMedia Pin
dene9997028-Dec-06 13:50
dene9997028-Dec-06 13:50 
AnswerRe: WMI Win32_DiskDrivePhysicalMedia Pin
ppnht23-May-09 9:32
ppnht23-May-09 9:32 
Questionthread-safe private variable Pin
Nick Alexeev28-Dec-06 13:48
professionalNick Alexeev28-Dec-06 13:48 
Are there any caveats in using ::WaitForSingleObject(…) with CMutex? MSDN article for ::WaitForSingleObject(…) says that it’s possible to use it with a mutex. However, the program hangs, when I try to Lock() the mutex.

Mutex is constructed together with a resource that it guards. The constructor for CResource is called in the main thread. Interestingly, the call to Lock() in the constructor succeeds.

CResource::CResource()
	: m_mtxParse(FALSE)	// initially, nobody owns this mutex
{
	m_iData = 0;			// guarded data
	// m_mtxParse.Lock();	// a call to Lock() succeeds here
}


Then the pointer to the resource object is passed to two (2) worker threads that manipulate and read the guarded data through two (2) methods.

called only by the 1st thread
// PURPOSE: a data manipulation procedure that takes time
void CResource::IncrementData()
{
	m_mtxParse.Lock();	// this always hangs.  CSingleLock snglLock(&m_mtxParse, TRUE) also always fails.
	for (int i = 0; i < 10; ++i)	// I want to change the data, whose thread-safety is being protected, gradually for debug purposes
	{
		++m_iData;
		::Sleep(100);		// artificially increase the time of this operation
	}
	m_mtxParse.Unlock();
}


called only by the 2nd thread:
// PURPOSE: an accessor method, thread-safe hopefully :)
int CResource::GetData()
{
	::WaitForSingleObject(m_mtxParse.m_hObject, INFINITE);	// if this line is commented out, the call to Lock() in IncrementData() succeeds
	return m_iData;
}


So, what are the limitation for ::WaitForSingleObject(…) with CMutex?

1. The purpose of mutex is to allow only one thread at a time to execute a certain code and to block all othre threads. I positively can make the mutex work this way, if I don't call ::WaitForSingleObject(...) on my mutex. Why does ::WaitForSingleObject(...) screw up the call to CMutex::Lock()? That's what's bugging me!

2. If I understand the ::WaitForSingleObject(...) correctly, it should be able to wait for a mutex. So I can make another thread (which never even tries to execute the code protected by the mutex) to wait until the other thread is done. I had acheived this functionality with CEvent, and I can, probably use a combination of CEvent and CMutex. But, I think, I should be able to wait for a CMutex and I don't need CEvent.

Do I have a wrong understanding of CMutex and ::WaitForSingleObject(...)?
If I have the right understanding, what's wrong with my code?

Cheers,
Nick

P.S. If you want to post an alternative solution (e.g. use CEvent instead of CMutex), please be so kind and do so only after you explain why the present approach will not work. I’m asking you to do this, because primarily I want a direct answer to my direct question. I’m learning multithreading, so my interest is academic (not commercial). I also know about some of the alternative solutions already. Thanks!
AnswerRe: thread-safe private variable Pin
Mark Salsbery28-Dec-06 14:59
Mark Salsbery28-Dec-06 14:59 
GeneralRe: thread-safe private variable Pin
Nick Alexeev28-Dec-06 17:32
professionalNick Alexeev28-Dec-06 17:32 
GeneralRe: thread-safe private variable Pin
Blake Miller2-Jan-07 11:11
Blake Miller2-Jan-07 11:11 
Questionhow to make a linux game Pin
active6628-Dec-06 13:23
active6628-Dec-06 13:23 
AnswerRe: how to make a linux game Pin
ThatsAlok28-Dec-06 23:14
ThatsAlok28-Dec-06 23:14 
QuestionHow to create a window in general? Pin
Jethro6328-Dec-06 10:09
Jethro6328-Dec-06 10:09 
AnswerRe: How to create a window in general? [modified] Pin
Mark Salsbery28-Dec-06 11:24
Mark Salsbery28-Dec-06 11:24 
AnswerRe: How to create a window in general? Pin
Mark Salsbery28-Dec-06 11:47
Mark Salsbery28-Dec-06 11:47 
GeneralRe: How to create a window in general? Pin
Jethro6328-Dec-06 13:32
Jethro6328-Dec-06 13:32 
GeneralRe: How to create a window in general? Pin
Mark Salsbery28-Dec-06 13:40
Mark Salsbery28-Dec-06 13:40 
GeneralRe: How to create a window in general? Pin
Jethro6328-Dec-06 13:59
Jethro6328-Dec-06 13:59 
GeneralRe: How to create a window in general? Pin
Mark Salsbery28-Dec-06 14:19
Mark Salsbery28-Dec-06 14:19 
GeneralRe: How to create a window in general? Pin
Jethro6328-Dec-06 14:27
Jethro6328-Dec-06 14:27 
GeneralRe: How to create a window in general? Pin
Mark Salsbery28-Dec-06 14:33
Mark Salsbery28-Dec-06 14:33 
GeneralRe: How to create a window in general? Pin
Mark Salsbery28-Dec-06 14:38
Mark Salsbery28-Dec-06 14:38 
GeneralRe: How to create a window in general? Pin
Jethro6328-Dec-06 14:45
Jethro6328-Dec-06 14:45 
GeneralRe: How to create a window in general? Pin
prasad_som28-Dec-06 19:41
prasad_som28-Dec-06 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.