信号量与线程. [英] Semaphore's with Threads.

查看:95
本文介绍了信号量与线程.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的应用程序中发生内存泄漏.当我使用信号量时,等待其他线程到达而被卡住的线程会给我造成内存泄漏.

如何释放卡住的线程以及如何知道其中有多少线程?

任何帮助将不胜感激.

提前谢谢.

Hello All,

I am having a memory leak in my application. When I use semaphore, the threads that are stuck waiting for other threads to arrive are giving me memory leak.

How do I release threads that are stuck and how do I know how many are in there?

Any help will be appreciated.

Thanks In advance.

void CTestManualThreadDlg::OnStartStop()
{
	m_EventEnd.ResetEvent();
	m_pMainThread = AfxBeginThread(MainThread, this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	m_pMainThread->m_bAutoDelete = FALSE;
	m_pMainThread->ResumeThread();
}
UINT CTestManualThreadDlg::MainThread(LPVOID p)
{

	CTestManualThreadDlg * Me = (CTestManualThreadDlg *)p;
	Me->MainThread();
	return 0;
}
void CTestManualThreadDlg::MainThread()
{
	static int m_mainvar = 0;
	CString Test;
	while(WAIT_TIMEOUT == ::WaitForSingleObject(m_EventEnd.m_hObject, 0))
	{
		m_mainvar++;
		Test.Format("%d", m_mainvar);
		m_MainThread.SetWindowText(Test);
		for (int x= 0; x<10; x++)
		{
			ChildThreadData *pData = new ChildThreadData;
			pData->pDlg = this;
			pData->ThreadNumber = x;
////THIS ARE SET FOR AUTODELETE....
			m_pChildrenThread[x] = AfxBeginThread(ChildThread, pData,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
			m_pChildrenThread[x]->ResumeThread();
		}
	}
	m_EventEnd.SetEvent();
}

UINT CTestManualThreadDlg::ChildThread(LPVOID p)
{
	ChildThreadData *pData = (ChildThreadData *)p;
	pData->pDlg->ChildThread(pData->ThreadNumber);
	delete pData;
	return 0;
}
void CTestManualThreadDlg::ChildThread(int ThreadNumber)
{
	CString Test;
	m_pMainThread->SuspendThread();
	m_ChildSemaphore.Lock();
	g_childvar1++;
	Test.Format("%d", g_childvar1);
	m_ChildThread1.SetWindowText(Test);
	g_childvar++;
	Test.Format("%d", g_childvar);
	m_ChildThread2.SetWindowText(Test);
	m_ChildSemaphore.Unlock();
	m_pMainThread->ResumeThread();
}
void CTestManualThreadDlg::OnExit()
{
	
	m_EventEnd.SetEvent();
	::WaitForSingleObject(m_EventEnd.m_hObject, INFINITE);
	m_pMainThread->Delete();
	OnOK();
}

推荐答案

这是错误的方法.如果由于线程一直在等待而使线程卡住,则这是一个称为死锁的问题.它可能比内存泄漏严重得多.您不应以释放"为目标.您应该防止它们.请参阅:
http://en.wikipedia.org/wiki/Deadlock [ http://en.wikipedia.org/wiki/Petri_net [ http://msdn.microsoft.com/en-us/library/ms810303.aspx [ ^ ].

现在,让我们回到内存泄漏.您的问题没有告诉我您已检测到内存泄漏的真正原因.您应该通过准确的诊断方法解决此问题.您可以使用称为"内存调试器"的工具类别中的某些开发工具.请检查在本文中找到的可用内存调试器的列表:
http://en.wikipedia.org/wiki/Memory_debugger [
This is a wrong approach. If you have threads stuck because they are waiting for each other forever, this is a problem called deadlock. It can be much more serious than your memory leaks. You should not aim to "release" them; you should prevent them. Please see:
http://en.wikipedia.org/wiki/Deadlock[^].

You can achieve this goal through accurate architecture and design of your system. One interesting and simple method of threaded system analysis is using the Petri net formalism. Please see:
http://en.wikipedia.org/wiki/Petri_net[^].

This excellent Microsoft article teaches us how to use it in practice:
http://msdn.microsoft.com/en-us/library/ms810303.aspx[^].

Now, let''s come back to memory leaks. Nothing in your question tells me that you have detected the real reasons for your memory leaks. You should approach this problem from accurate diagnostics. You can use some development tool from the class of tools called "memory debugger". Please check the list of available memory debuggers found in this article:
http://en.wikipedia.org/wiki/Memory_debugger[^].

Good luck,
—SA


这篇关于信号量与线程.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆