Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Waiting for the thread end

Waiting for the thread end

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionc++visual-studio
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gomez_a
    wrote on last edited by
    #1

    (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

    R S 2 Replies Last reply
    0
    • G gomez_a

      (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

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      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!

      1 Reply Last reply
      0
      • G gomez_a

        (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

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        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 of CWinThread 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 the CWinThread objects when you're done with them - deleting one doesn't stop the thread running (if it's running). Steve

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups