AfxBeginThread和类成员函数:使用朋友函数的解决方法问题 [英] AfxBeginThread and class member function: problem with workaround using friend function

查看:452
本文介绍了AfxBeginThread和类成员函数:使用朋友函数的解决方法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经在Google上搜索了所有内容,所有使我一开始就这样做的原因.实际的线程被定义为好友,因为AfxBeginThread仅采用静态函数,并且指向类实例的指针作为参数传递.
但是我看不出为什么这是错误的.
非常高兴为您提供解释/正确的方法.

现在,我将其编写为最大程度简化的版本,并且像这样显然显然没有多大意义.
但是我真正想做的是拥有一个线程,该线程作为参数接收指向类实例的指针,从而可以操纵类变量.听起来很简单,不是吗?

好吧,我尝试执行此操作的方法可以编译,但无法运行,因为 bold
中打印的行中存在错误 Unhandled exception at 0x0041186d in simpleThread.exe: 0xC0000005: Access violation reading location 0xccccccd0.


Ok, I have googled this and everything which made me do this the way I try in the first place. The actual thread is defined as friend because AfxBeginThread only takes static functions, and a pointer to the class instance is passed as argument.
But I just can''t see why this is wrong.
Really glad for an explanation / correct way to do it.

Now, I wrote this as a maximally stripped down version and like this it obviously doesn''t make a lot of sense.
But what I really want to do is have a thread which, as argument, receives a pointer to a class instance, and can thus manipulate class variables. Sounds easy, no?

Well, the way I tried to do it, it compiles but doesn''t run, following error in the line printed in bold
Unhandled exception at 0x0041186d in simpleThread.exe: 0xC0000005: Access violation reading location 0xccccccd0.


class testClass{
public:
	
	testClass();

	CEvent* pEvent;
	CWinThread* pThread;
	friend UINT ThreadProc(LPVOID lpParameter);

	void startThread();
        void sendEvent();
};


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{

   testClass myInstance;
   myInstance.startThread();
   Sleep(2000);
   myInstance.sendEvent();
   return 0;
}

UINT ThreadProc( LPVOID lpParameter )
{
	testClass* instance = (testClass*)(lpParameter);
	WaitForSingleObject( instance->pEvent->m_hObject, INFINITE );
	return 0L;
}

testClass::testClass(){
	CEvent* pEvent = new CEvent( FALSE, FALSE );
}

void testClass::startThread()
{
	pThread = ::AfxBeginThread( ThreadProc, this, 0, 0, 
                                    CREATE_SUSPENDED, NULL);
	pThread->m_bAutoDelete = FALSE; 
	pThread->ResumeThread();
}
void testClass::sendEvent()
{
    pEvent->SetEvent();
}



PS:我的代码基于 http://msdn.microsoft.com /en-us/library/efk30beh%28v=vs.80%29.aspx [



P.S.: My code is based on http://msdn.microsoft.com/en-us/library/efk30beh%28v=vs.80%29.aspx[^] which is very similar, except they don''t try to wrap it all up in a class.

Many thanks for solutions / suggestions.

推荐答案

我不认为您应该以这种方式解决您的问题……您为什么想要一个独立的线程来直接操纵您不同类别的数据成员?您需要学习使用线程安全的方法来执行此操作.

还有一件事...
永远不要将WaitForSingleObject()用于无限超时!
I don''t think you should approach your problem in this manner... Why would you ever want an independent thread to directly manipulate your data members of a different class? You need to learn to use thread safe ways of doing this.

One more thing...
NEVER use WaitForSingleObject() with an INFINITE timeout!


这篇关于AfxBeginThread和类成员函数:使用朋友函数的解决方法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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