Waiting for the thread end
-
(VS 2005 MFC) I have a list of 5 threads, for example: struct T_PARAMS { HWND hWnd; // a handle to dialog window, using in thread }; int thread_count = 5; // I have 5 threads CWinThread *pThread; // pointer to thread CList mList; // list of the pointers to threads int lp = 0; // First I create 5 stopped threads: for(lp = 0; lp < thread_count; lp++) { params = new T_PARAMS(); params->hWnd = m_hWnd; pThread = AfxBeginThread(ThreadFunc, params, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); pThread->m_bAutoDelete = true; mList.AddTail(pThread); } Now, when I have a list of pointers to threads, I want run threads, but I want to do it in this way: run first thread (mList[0]), wait so long how the first thread is working, next run second thread (mList[1]) and so on. For example: POSITION pos; CWinThread *wsk = NULL; pos = mList.GetHeadPosition(); for(lp = 0; lp < thread_count; lp++) { wsk = (CWinThread *) mList.GetNext(pos); wsk->ResumeThread(); // [*] now wait until thread is working - how to do it? } My threads are working properly, I use one semaphor to allow works only 1 thread at once, but I don't know how can I do - in line [*], that programm will wait after wsk->ResumeThread() until this thread is finished his work? Regards mwgomez Poland
-
(VS 2005 MFC) I have a list of 5 threads, for example: struct T_PARAMS { HWND hWnd; // a handle to dialog window, using in thread }; int thread_count = 5; // I have 5 threads CWinThread *pThread; // pointer to thread CList mList; // list of the pointers to threads int lp = 0; // First I create 5 stopped threads: for(lp = 0; lp < thread_count; lp++) { params = new T_PARAMS(); params->hWnd = m_hWnd; pThread = AfxBeginThread(ThreadFunc, params, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); pThread->m_bAutoDelete = true; mList.AddTail(pThread); } Now, when I have a list of pointers to threads, I want run threads, but I want to do it in this way: run first thread (mList[0]), wait so long how the first thread is working, next run second thread (mList[1]) and so on. For example: POSITION pos; CWinThread *wsk = NULL; pos = mList.GetHeadPosition(); for(lp = 0; lp < thread_count; lp++) { wsk = (CWinThread *) mList.GetNext(pos); wsk->ResumeThread(); // [*] now wait until thread is working - how to do it? } My threads are working properly, I use one semaphor to allow works only 1 thread at once, but I don't know how can I do - in line [*], that programm will wait after wsk->ResumeThread() until this thread is finished his work? Regards mwgomez Poland
Have a look at Joe Newcomer's article about worker threads. It's excellent! He'll show you how to all of this the correct way here[^]. I urge you to read the complete article since it will make you aware of problems you have to know how to deal with when doing multithreading. Hope this helps -- Roger
It's suppose to be hard, otherwise anybody could do it!
-
(VS 2005 MFC) I have a list of 5 threads, for example: struct T_PARAMS { HWND hWnd; // a handle to dialog window, using in thread }; int thread_count = 5; // I have 5 threads CWinThread *pThread; // pointer to thread CList mList; // list of the pointers to threads int lp = 0; // First I create 5 stopped threads: for(lp = 0; lp < thread_count; lp++) { params = new T_PARAMS(); params->hWnd = m_hWnd; pThread = AfxBeginThread(ThreadFunc, params, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); pThread->m_bAutoDelete = true; mList.AddTail(pThread); } Now, when I have a list of pointers to threads, I want run threads, but I want to do it in this way: run first thread (mList[0]), wait so long how the first thread is working, next run second thread (mList[1]) and so on. For example: POSITION pos; CWinThread *wsk = NULL; pos = mList.GetHeadPosition(); for(lp = 0; lp < thread_count; lp++) { wsk = (CWinThread *) mList.GetNext(pos); wsk->ResumeThread(); // [*] now wait until thread is working - how to do it? } My threads are working properly, I use one semaphor to allow works only 1 thread at once, but I don't know how can I do - in line [*], that programm will wait after wsk->ResumeThread() until this thread is finished his work? Regards mwgomez Poland
First when you create the thread but before you let it run make sure you do the following (you will have to create it suspended):
pThread->m_bAutoDelete = FALSE;
There is no point in having a collection ofCWinThread
pointers if you have no way of knowing if the object pointed to has been delete or not! Now you can wait for a thread to exit using the Win32 API as follows:WaitForSingleObject(pThread->m_hThread, INFINITE);
Remember we've turned auto deletion off so you have to remember to delete theCWinThread
objects when you're done with them - deleting one doesn't stop the thread running (if it's running). Steve