Subj : Re: Return value from worker thred when the working thread must be suspend after calculation??? To : comp.os.ms-windows.programmer.win32,microsoft.public.vc.language,comp.programming.threads From : Roy Fine Date : Mon Aug 29 2005 09:47 pm pattreeya you get a return code when the thread function returns - else you don't. If you want to set the thread return code, then use the return statement or the ExitThread API call and specify a return value. returning from the thread destroys the thread object and it is of no further use. if you don't want to return from the thread, then the thread return value is not relevant. Do not call SuspendThread - just return with the appropriate return value, or use some sort of signaling object and one of the WaitForXXXObject API functions. Others may jump all over this, but I can think of no good purpose for SuspendThread in common multithreading desktop applications.... if you want to keep the worker thread active, then you need to look at a total application re-design. Start the worker thread, and have that thread function wait for a signal to go to work - use a mutex or an event. when the calculation are done, have the worker thread signal the main thread using another signaling object (mutex or event). figure out some clever mechanism to pass info into and to fetch info out of the thread function - there are many ways to do this; consider a queue of some fashion or an synchronized common data area (use CriticalSection to prevent concurrent access) regards Roy Fine wrote in message news:1125357370.543175.203810@g43g2000cwa.googlegroups.com... > Hello, > > it will be a very kind of you if someone can help me with the > following problem. > > > After finish calling "Calculate" function, I would like > to send the result to the main thread and suspend the working thread. > how can i get the the return value from this threadproc to be used by > the main thread? > > > > My idea for the code goes like this: > > struct ST_CALC{ > int start; > int end; > } > > > int Calculate(bool flag,int start, int end) > { > int ret_value = 0; > if( ! flag ){ > for(int i=start; i ++ret_value; > > } > else{ > for(int i=start; i ++ret_value; > > } > return ret_value; > > } > DWORD WINAPI CalcThreadProc(void * param) > { > ST_CALC* st_calc =(ST_CALC *)(param); > > while(1){ > if( ::WaitForSingleObject(evShouldStop, 0) == WAIT_OBJECT_0) > break; > > int ret = Calculate(false, st_calc->start, st_calc->end); > > ::SuspendThread(hRtCalcThrd); > //??? How can I get return value, "ret", to be use by the calling > thread? Should I call put the SuspendThread outside this func after > get the return value? > > } > //???? > ::ExitThread(0); > } > > int MyCalculate(int start, int end) > { > result = Calculate(true, start, end); > > if(result){ > ::ResumeThread(hRtCalcThrd); > } > //??? I would like to use the return value from threadproc > inside this function, how can we get its return value?? > > return result; > } > > void Start(int start, int end) > { > ST_CALC* p; > p->start = start; > p->end = end; > > hRtCalcThrd = ::CreateThread(0, 0, CalcThreadProc, p, > CREATE_SUSPENDED, m_idThread); > > result = MyCalculate(); > > } > .