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. Problem with WaitForSingleObject

Problem with WaitForSingleObject

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 Posts 8 Posters 1 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.
  • A Offline
    A Offline
    Amol Jaikar
    wrote on last edited by
    #1

    CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me

    R L CPalliniC A F 6 Replies Last reply
    0
    • A Amol Jaikar

      CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      And what does your thread function look like?

      It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.

      1 Reply Last reply
      0
      • A Amol Jaikar

        CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You should capture the return value from WaitForSingleObject to see why it did not wait. You also need to check that your worker thread is actually doing some work rather than returning immediately. NOTE: Please enclose your code snippet in <pre></pre> tags (use the "code block" button) to make it more readable.

        Just say 'NO' to evaluated arguments for diadic functions! Ash

        1 Reply Last reply
        0
        • A Amol Jaikar

          CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Wait on the thread handle, not on the pointer to the thread class.

          ...byte till it megahertz... my donation to web rubbish

          CPalliniC C 2 Replies Last reply
          0
          • A Amol Jaikar

            CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me

            CPalliniC Online
            CPalliniC Online
            CPallini
            wrote on last edited by
            #5

            As already suggested, you have to check the return value of every API function you call, otherwise... troubles. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • L Lost User

              Wait on the thread handle, not on the pointer to the thread class.

              ...byte till it megahertz... my donation to web rubbish

              CPalliniC Online
              CPalliniC Online
              CPallini
              wrote on last edited by
              #6

              Good point. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • A Amol Jaikar

                CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me

                A Offline
                A Offline
                Arman S
                wrote on last edited by
                #7

                OK, here is your problem: you are passing the returned value (CWinThread pointer) from AfxBeginThread to WaitForSingleObject, which is not correct. Instead, you should pass the HANDLE defined inside those m_pcThread1 and m_pcThread2. Like so: WaitForSingleObject(m_pcThread1->m_hThread,INFINITE); WaitForSingleObject(m_pcThread2->m_hThread,INFINITE);

                -- Arman

                1 Reply Last reply
                0
                • A Amol Jaikar

                  CWinThread* m_pcThread1; CWinThread* m_pcThread2; int a1[100], a2[100],a3[200]; para* a; int* a9; para* b; int* b9; int* ptr = a3; a = new para; b = new para; a->main_ptr = a1; a->val = 1; b->main_ptr = a2; b->val = 0; m_pcThread1 = AfxBeginThread(WorkerThreadProc,a,THREAD_PRIORITY_NORMAL,0,0,NULL); m_pcThread2 = AfxBeginThread(WorkerThreadProc,b,THREAD_PRIORITY_NORMAL,0,0,NULL); // MessageBox("Thread Started"); // if(WaitForSingleObject(m_pcThread1, INFINITE) == WAIT_OBJECT_0) WaitForSingleObject(m_pcThread1,INFINITE); WaitForSingleObject(m_pcThread2,INFINITE); a9 = a->main_ptr; b9 = b->main_ptr; for(int i = 0; i<100;i++) { *ptr = *a9; *(ptr+100) = *b9; ptr++; a9++; b9++; } MessageBox("Calculated"); ------------------------------------------------------------ Above code is not waiting for thread to complete. I can not understand what is the problem? please help me

                  F Offline
                  F Offline
                  Frank Seidler
                  wrote on last edited by
                  #8

                  Hello, WaitForSingleObject takes a handle as first argument. You are passing a pointer to a CWinThread. Changing your code to

                  WaitForSingleObject( m_pcThread1->m_hThread, INFINITE );
                  WaitForSingleObject( m_pcThread2->m_hThread, INFINITE );

                  should solve your issue. Regards Frank

                  1 Reply Last reply
                  0
                  • L Lost User

                    Wait on the thread handle, not on the pointer to the thread class.

                    ...byte till it megahertz... my donation to web rubbish

                    C Offline
                    C Offline
                    Code o mat
                    wrote on last edited by
                    #9

                    I wonder why that compiles for him...

                    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world < >Nothing is free in the universe.<

                    A 1 Reply Last reply
                    0
                    • C Code o mat

                      I wonder why that compiles for him...

                      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world < >Nothing is free in the universe.<

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

                      it compiles 'cause Microsoft made a horrible decision with kernel handles in NT - they made them all void pointers so any type of pointer is compatible with them. You can pass any old rubbish pointer into most kernel functions where it expects a kernel handle and the compiler can't tell the difference. Ironically this is unlike GDI handles which they'd already made specific structure pointers in Windows 3.1 - 3 years previously. Such is life... [1] If you're prone to messing this sort of thing up a lot (as I am) then you can make things a bit more type safe in C++ by defining an overload of WaitForSingleObject: DWORD WaitForSingleObject( CWinThread *thread_ptr, DWORD msec_to_wait ) { return WaitForSingleObject( *thread_ptr, msec_to_wait ); } which will catch this sort of error. It's a right pain in the bum to have to that though and you have to make sure that you include your API wrappers otherwise you're back to square one (and there's the old catch-22 - if you know enough to use the typesafe wrappers you know enough to not need to use them). Cheers, Ash [1] I suppose the difference was that in Windows 3.1 if you got a handle wrong you had to reboot as you'd trashed the system, in NT you'd just mess up a process.

                      C 1 Reply Last reply
                      0
                      • A Aescleal

                        it compiles 'cause Microsoft made a horrible decision with kernel handles in NT - they made them all void pointers so any type of pointer is compatible with them. You can pass any old rubbish pointer into most kernel functions where it expects a kernel handle and the compiler can't tell the difference. Ironically this is unlike GDI handles which they'd already made specific structure pointers in Windows 3.1 - 3 years previously. Such is life... [1] If you're prone to messing this sort of thing up a lot (as I am) then you can make things a bit more type safe in C++ by defining an overload of WaitForSingleObject: DWORD WaitForSingleObject( CWinThread *thread_ptr, DWORD msec_to_wait ) { return WaitForSingleObject( *thread_ptr, msec_to_wait ); } which will catch this sort of error. It's a right pain in the bum to have to that though and you have to make sure that you include your API wrappers otherwise you're back to square one (and there's the old catch-22 - if you know enough to use the typesafe wrappers you know enough to not need to use them). Cheers, Ash [1] I suppose the difference was that in Windows 3.1 if you got a handle wrong you had to reboot as you'd trashed the system, in NT you'd just mess up a process.

                        C Offline
                        C Offline
                        Code o mat
                        wrote on last edited by
                        #11

                        Damn, you are right, i should have thought of that...i must be getting old and senile...what are we talking about again? :)

                        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world < >Nothing is free in the universe.<

                        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