15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


About threads in VC++

Maheshwar Gundelly -- gundelly@enuxsa.eas.asu.edu
Sunday, April 14, 1996

Hi,
Environment : Windows NT Version :3.51 VC++ 4.0


	I want to create threads in my VC++ program. Does any one can help 
me with some code as how to create threads in VC++ with AfxBeginThread. 

	I want to have thread function and calling functions to be the
members functions of a class. 

Note : Need an example if possible

Thank you,
					Mahesh



Mike Blaszczak -- mikeblas@msn.com
Wednesday, April 17, 1996

[Mini-digest: 6 responses]

From: 	owner-mfc-l@netcom.com on behalf of Maheshwar Gundelly
Sent: 	Sunday, April 14, 1996 21:16

> Environment : Windows NT Version :3.51 VC++ 4.0

Thanks.

> I want to create threads in my VC++ program. Does any one can help
> me with some code as how to create threads in VC++ with AfxBeginThread. 

So, you're actually writing an MFC program and not just a C++ program?  If 
you're writing just a plain C++ program, calling AfxBeginThread() is not a 
good idea.  You shouldn't use AfxBeginThread() unless you're using MFC in your 
application.

> I want to have thread function and calling functions to be the
> members functions of a class. 

You can't do that.  There's no way for the thread function to get a "this" 
pointer.  You have to carefully work around that issue.  I'd recomend, 
instead, that you use the _real_ MFC method of calling AfxBeginThread() with 
the RUNTIME_CLASS() info for a class you'd like to create.

> Note : Need an example if possible

Try the MUTEXES sample on your Visual C++ CD.

.B ekiM
TCHAR sz[] = _T("These words are my own: I don't speak for Microsoft");
-----From: Clarence Chiang 

Anyway, back to the question ... I think AfxBeginThread is overloaded, 
one version will take a pointer to a function, a pointer to the data 
which is the parameter(s) to the function and some other arguments as 
well. I will probably do something like this:

class Worker {

  public:

    static void TheWorkingFunc( ... ) { ... } // I think this function 
must take a certain
                                              // parameter in order to 
pass to AfxBeginThread
}

  ...


Worker w( ... );

AfxBeginThread( ..., &Worker::TheWorkingFunc, (void *)&w, ...);

..


That way you can group each thread in to different class.
Hope that helps.

Clarence 
Spider Island Software

-----From: Ranjiv Sharma 


Environment : Windows NT Version :3.51 VC++ 4.0

>> I want to create threads in my VC++ program. Does any one can help
>> me with some code as how to create threads in VC++ with   
AfxBeginThread.

There are 2 overloaded AfxBeginThread() fnuctions. One is for a UI   
thread, to which you need to pass the runtime class of your thread. The   
other, which is probable what you want to use is prototyped as follows

CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam,   
int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD   
dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );

The Thread function is of type AFX_THREADPROC which in turn is defined as

typedef UINT (AFX_CDECL *AFX_THREADPROC)(LPVOID);

You are pretty much limited to using a function that matches this   
prototype as your thread function. You can probably simulate what you   
want by doing something like this

class CMyClass
{
    public:
 UINT MyThreadFunction();
 void MyThreadCallingFunction();
}

void CMyClass::MyThreadCallingFunction()
{
 //pass the object as the parameter to the thread function
 AfxBeginThread(NonClassThreadFunction, this);
}


UINT AFX_CDECL NonClassThreadFunction(LPVOID pObj)
{
 //pObj contains your object
 CMyClass* pThis = (CMyClass*) pObj;
 pObj->MyThreadFunction();
}

  Basically, you pass the object as the parameter in the Thread function,   
and then turn around
and call a member function of the object in the Thread function.

Hope that helps
 -Ranjiv


-----From: Steven Youngs 

Well, in order for the thread function (I'm talking worker threads here by the way) to
be a class member, you will have to make it static.

Here is a simple example

class CFoo
{
    CWinThread *m_pThread;
    void StartFooThread();
    static UINT FooWorker(LPVOID pParam);
}

void CFoo::StartFooThread()
{
    m_pThread = AfxBeginThread(FooWorker, NULL);
}

UINT CFoo::FooWorker(LPVOID pParam)
{
    TRACE("I'm a worker thread\n");
}

NOTE: I've omitted all error checking and resource management code :-)
The pParam argument allows you to pass data into your worker thread.
Hope this helps.

Steve.
_____________
Steven Youngs
NC Graphics (Cambridge) Ltd.

-----From: craigtt@ccmail.PE-Nelson.COM
     
Mahesh,

You can make the ThreadProc of the thread a member function of a class if the 
function is "static".  If the thread needs to operate on a particular instance 
of the class,  you can pass a pointer to the instance as the parameter to the 
ThreadProc.

If you want a class to appear to be operating independently in its own thread, 
consider deriving the class from CWinThread.  While CWinThread is documented to 
be for user interface threads, you can override the Run function which is 
essentially the ThreadProc and turn it into a simple worker thread with the 
advantage that you have full access to all the members of the class.

Tim Craig
PE-Nelson

-----From: Vladislav Mamistvalov 

Try read MS Book online "Multithreading:Creating User-Interface Threads" =
and "Multithreading:Creating Worker Threads" the last topic have =
example.



Tony D. Abel -- tabel@campus.mci.net
Thursday, April 18, 1996



Thread snippet:

This thread is started to provide communication between a modeless real
time status dialog and a hardware controller object. TempBKThread is a 
global function that handles messages for event notification.

//	Start background thread
		m_pTempThread = AfxBeginThread( (AFX_THREADPROC)TempBKThread,
		(LPVOID)this, THREAD_PRIORITY_LOWEST );


//	----------------------------------------------------------------------
//	Background thread which handles commands to the controller
//	----------------------------------------------------------------------
UINT TempBKThread( LPVOID pParam )
{
	CMyDlg *pDlg = (CMyDlg*)pParam;
	const	int	iTimeOut = 1500;	//	1.5 sec.
	
CTempInterface* pTempCtrl = 
	DIFFRACT.GetTempInterface( CDiffract::kAccess_ReadWrite, pDlg );
	ASSERT( pTempCtrl );	

	pDlg->m_dThreadSetPoint = pTempCtrl->GetTemp();	

	for(;;)
	{
	  try
	  {
            while( ::WaitForSingleObject( pDlg->m_hCommandEvent, iTimeOut )
				== WAIT_TIMEOUT )
	    {
		//	Poll the controller
		/*
		Do controller work
		*/				
            }

	//	We have been signaled by the dialog
		switch( pDlg->m_iThreadAction )
		{
			case CMyDlg::eMSG_ACTION_RAMP:
			//	Start a ramp
			/*
			Do ramp work
			*/
			break;

			case CMyDlg::eMSG_ACTION_DO_SOMETHING:
			/*
			Process more messages if you like
			*/	break;

// This ends the thread upon shutdown of the controller operation.

//	Wait for thread to terminate
		DWORD	dwExitCode = STILL_ACTIVE;
		while( dwExitCode == STILL_ACTIVE )
		{
			Sleep( 200 );
			
			VERIFY( ::GetExitCodeThread( m_pTempThread->m_hThread,
				&dwExitCode ) );
		}

		//	delete the thread
		delete m_pTempThread;


I hope this helps you to get started.

Sincerely,

Tony D. Abel


At 09:16 PM 4/14/96 -0700, you wrote:
>Hi,
>Environment : Windows NT Version :3.51 VC++ 4.0
>
>
>	I want to create threads in my VC++ program. Does any one can help 
>me with some code as how to create threads in VC++ with AfxBeginThread. 
>
>	I want to have thread function and calling functions to be the
>members functions of a class. 
>
>Note : Need an example if possible
>
>Thank you,
>					Mahesh
>
>





| Вернуться в корень Архива |