Click to See Complete Forum and Search --> : returning CString


tasoss
November 8th, 2004, 12:44 PM
Hello. I have some questions about threads.

fooa is a public Cstring.

CString CConnection::conne(void)
{
DATA *md = new DATA;

CWinThread* pThread=AfxBeginThread(ThreadFunc,this);

return fooa;
}


UINT CConnection::ThreadFunc(LPVOID pParam)
{
CConnection *fa=(CConnection *)pParam;//Parama;

………………………………………………………..

fa->fooa=ge;


How is it possible to pass references in pParam???
I mean by fa->fooa=ge; I change fooa but the value of fooa remains the same.If I have used refereces I could change the value of fooa and return fooa; would return the correct CString.I know it’s possible to pass a structure as pParam and process it in ThreadFunc(DATA *md = new DATA;).My question is how can I keep the changes I have made in structure’s members,from inside ThreadFunc.

Generally how can the thread function return a value? The specific thread I have written connects to a server, gets an html file which is stored in a CString variable. I want ,somehow, to return this CString so it can be processed by another class.

MikeAThon
November 8th, 2004, 04:27 PM
The value of fooa is clearly being changed by the thread, but it's not changed by the time that the CConection::conne function returns it.

In other words, the CConection::conne function starts the thread and then immediately returns fooa, before the thread is even able to commence execution. At some point in time, long after CConnection::conne has already returned, the thread executes the line fa->fooa=ge and changes the value of fooa, but as far as the conne function is concerned, the change occurs too late.

Mike

tasoss
November 8th, 2004, 04:58 PM
Thx.I fixed it.Thank you for the detail.
So,the only way for a thread function to return anything is by passing a structure via pParam.But i have to wait...Is it right?
ps:i added a one second delay.i don't think that's very effective for the program.is there any way to make CWinThread* pThread = AfxBeginThread(ThreadFunc,this);
not to return until the thread finishes its execution?

MrViggy
November 8th, 2004, 05:12 PM
Sure, but why use a thread then? ;)

Check out WaitForSingleObject:
CWinThread* pThread = AfxBeginThread(ThreadFunc,this);
WaitForSingleObject(pThread->m_hThread, INFINITE);
This should cause the thread that kicks off the second thread to wait until that new thread is finished. You can also set it to wait for a specific amount of time, then continue. See the MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitforsingleobject.asp

Viggy

MikeAThon
November 8th, 2004, 06:55 PM
Sure, but why use a thread then? ;)

Check out WaitForSingleObject:
CWinThread* pThread = AfxBeginThread(ThreadFunc,this);
WaitForSingleObject(pThread->m_hThread, INFINITE);
This should cause the thread that kicks off the second thread to wait until that new thread is finished. You can also set it to wait for a specific amount of time, then continue. See the MSDN:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/waitforsingleobject.asp

Viggy
To be extra-cautious, you need to set the m_bAutoDelete flag to false, or there is a very tiny chance that just after the thread is started, but before the main thread starts its WFSO call, the worker thread is run to completion and is deleted. In that case, the WFSO will never be signalled, since the thread handle is already closed. Try this:
CWinThread* pThread = AfxBeginThread(ThreadFunc,this,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pThread->m_bAutoDelete=FALSE;
pThread->Resume();
WaitForSingleObject(pThread->m_hThread, INFINITE);

However, I also totally agree that a thread is not needed here, if the main thread must wait anyway for the thread to run to completion.

Mike