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. Second worker thread should start after first has finished it task.

Second worker thread should start after first has finished it task.

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
9 Posts 7 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.
  • L Offline
    L Offline
    learningvisualc
    wrote on last edited by
    #1

    Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance

    K E C X A 5 Replies Last reply
    0
    • L learningvisualc

      Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance

      K Offline
      K Offline
      KingsGambit
      wrote on last edited by
      #2

      In the second thread you can use WaitForSingleObject() to wait for the first thread's handle to complete.

      1 Reply Last reply
      0
      • L learningvisualc

        Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance

        E Offline
        E Offline
        Eytukan
        wrote on last edited by
        #3

        1. Start 1st thread. 2. Wait for it to finish. 3. Start 2nd thread.

        learningvisualc wrote:

        my problem is

        ??

        Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.

        1 Reply Last reply
        0
        • L learningvisualc

          Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          In addition to the previous answer: is there a particular reason why you want to use a second thread ? You have to wait until the first thread is finished, so why don't you simply execute that task in the same thread as the first one. I don't really see an added value of starting a second thread when the first one terminates.

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          X 1 Reply Last reply
          0
          • L learningvisualc

            Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance

            X Offline
            X Offline
            Xie Jinghui
            wrote on last edited by
            #5

            //
            #include "stdafx.h"

            #include "iostream"
            using namespace std;

            HANDLE hThread1 = NULL; //worker thread 1
            HANDLE hThread2 = NULL; //worker therad 2

            HANDLE hEvt1 = NULL;

            DWORD __stdcall ThreadProc_1( LPVOID lpParameter )
            {
            // populating the list ...
            for(int i = 0; i<10; i++)
            {
            cout<<"populating the list -- "<<i<<endl;
            }

            // after populate the list , set event, then the worker thread 2 will stop waiting and do operation on the list...
            ::SetEvent(hEvt1);
            
            return 0;
            

            }

            DWORD __stdcall ThreadProc_2( LPVOID lpParameter )
            {
            // wait
            while(::WaitForSingleObject(hEvt1, 5) == WAIT_TIMEOUT)
            {
            //keep waiting ...
            Sleep(5);
            continue;
            }

            // To do operation on that list...
            for(int i = 0; i<10; i++)
            {
            	cout<<"doing operation on that list -- "<<i<<endl;
            }
            
            return 0;
            

            }

            int _tmain(int argc, _TCHAR* argv[])
            {
            hEvt1 = CreateEvent(NULL, TRUE, FALSE, NULL);

            //Create the two worker threads
            hThread1 = CreateThread(NULL, 0, ThreadProc\_1, NULL, 0, NULL);
            hThread2 = CreateThread(NULL, 0, ThreadProc\_2, NULL, 0, NULL);
            
            // waiting , 
            while(::WaitForSingleObject(hThread2, 5) == WAIT\_TIMEOUT)
            {
            	Sleep(5);
            	continue;
            }
            
            if(hThread1)
            	::CloseHandle(hThread1);
            if(hThread2)
            	::CloseHandle(hThread2);
            
            if(hEvt1)
            	::CloseHandle(hEvt1);
            
            return 0;
            

            }

            S 1 Reply Last reply
            0
            • C Cedric Moonen

              In addition to the previous answer: is there a particular reason why you want to use a second thread ? You have to wait until the first thread is finished, so why don't you simply execute that task in the same thread as the first one. I don't really see an added value of starting a second thread when the first one terminates.

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              X Offline
              X Offline
              Xie Jinghui
              wrote on last edited by
              #6

              quite right

              1 Reply Last reply
              0
              • X Xie Jinghui

                //
                #include "stdafx.h"

                #include "iostream"
                using namespace std;

                HANDLE hThread1 = NULL; //worker thread 1
                HANDLE hThread2 = NULL; //worker therad 2

                HANDLE hEvt1 = NULL;

                DWORD __stdcall ThreadProc_1( LPVOID lpParameter )
                {
                // populating the list ...
                for(int i = 0; i<10; i++)
                {
                cout<<"populating the list -- "<<i<<endl;
                }

                // after populate the list , set event, then the worker thread 2 will stop waiting and do operation on the list...
                ::SetEvent(hEvt1);
                
                return 0;
                

                }

                DWORD __stdcall ThreadProc_2( LPVOID lpParameter )
                {
                // wait
                while(::WaitForSingleObject(hEvt1, 5) == WAIT_TIMEOUT)
                {
                //keep waiting ...
                Sleep(5);
                continue;
                }

                // To do operation on that list...
                for(int i = 0; i<10; i++)
                {
                	cout<<"doing operation on that list -- "<<i<<endl;
                }
                
                return 0;
                

                }

                int _tmain(int argc, _TCHAR* argv[])
                {
                hEvt1 = CreateEvent(NULL, TRUE, FALSE, NULL);

                //Create the two worker threads
                hThread1 = CreateThread(NULL, 0, ThreadProc\_1, NULL, 0, NULL);
                hThread2 = CreateThread(NULL, 0, ThreadProc\_2, NULL, 0, NULL);
                
                // waiting , 
                while(::WaitForSingleObject(hThread2, 5) == WAIT\_TIMEOUT)
                {
                	Sleep(5);
                	continue;
                }
                
                if(hThread1)
                	::CloseHandle(hThread1);
                if(hThread2)
                	::CloseHandle(hThread2);
                
                if(hEvt1)
                	::CloseHandle(hEvt1);
                
                return 0;
                

                }

                S Offline
                S Offline
                Sauro Viti
                wrote on last edited by
                #7
                1. Xie Jinghui wrote:

                  while(::WaitForSingleObject(hEvt1, 5) == WAIT_TIMEOUT)
                  {
                  //keep waiting ...
                  Sleep(5);
                  continue;
                  }

                  This has the same effect than:

                  ::WaitForSingleObject(hEvt1, INFINITE);

                  but is more complex and less efficient as the OS every 5 seconds schedule the thread to be runt again; using a wait timeout of INFINITE is better because the OS suspend the thread and resume it only when the eventis signeled.

                2. You don't really need to create an event: the second thread could simply wait for the first thread to terminate

                  ::WaitForSingleObject(hThread1, INFINITE);

                X 1 Reply Last reply
                0
                • S Sauro Viti
                  1. Xie Jinghui wrote:

                    while(::WaitForSingleObject(hEvt1, 5) == WAIT_TIMEOUT)
                    {
                    //keep waiting ...
                    Sleep(5);
                    continue;
                    }

                    This has the same effect than:

                    ::WaitForSingleObject(hEvt1, INFINITE);

                    but is more complex and less efficient as the OS every 5 seconds schedule the thread to be runt again; using a wait timeout of INFINITE is better because the OS suspend the thread and resume it only when the eventis signeled.

                  2. You don't really need to create an event: the second thread could simply wait for the first thread to terminate

                    ::WaitForSingleObject(hThread1, INFINITE);

                  X Offline
                  X Offline
                  Xie Jinghui
                  wrote on last edited by
                  #8

                  Thank you, :) I always overlook the details,,, the program will be more efficient,

                  1 Reply Last reply
                  0
                  • L learningvisualc

                    Hi all, I have two worker thread. one thread is populating the list and second thread is doing operation on that list. my problem is i want that after i have populated the list then only second thread should start. How can i do it? Thanks in advance

                    A Offline
                    A Offline
                    Aescleal
                    wrote on last edited by
                    #9

                    Make it single threaded - threads are only useful when doing parallel operatations, they're only overhead otherwise. Cheers, Ash

                    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