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. WaitForMultipleObjects( ) and thread object

WaitForMultipleObjects( ) and thread object

Scheduled Pinned Locked Moved C / C++ / MFC
databasedata-structureshelptutorialquestion
4 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.
  • J Offline
    J Offline
    Jin Yao
    wrote on last edited by
    #1

    For example: ..... // Create 2 threads and save their handles to hThread1, hThread2 rgHandles[0] = hThread1; rgHandles[1] = hThread2; while (TRUE) { dwIndex = ::WaitForMultipleObjects(2, rgHandles, FALSE, INFINITE); dwIndex -= WAIT_OBJECT_0; ...... // farther process at here } When the thread1 is terminated, the state of thread1 is set to signaled, then WaitForMultipleObjects() returns the index 0 of the array. But when the thread2 is terminated, the WaitForMultipleObjects() still returns the index 0 not index 1. I want WaitForMultipleObjects() to return the correct index, so I can process for this terminated thread. who can help me? Thanks a lot

    D 1 Reply Last reply
    0
    • J Jin Yao

      For example: ..... // Create 2 threads and save their handles to hThread1, hThread2 rgHandles[0] = hThread1; rgHandles[1] = hThread2; while (TRUE) { dwIndex = ::WaitForMultipleObjects(2, rgHandles, FALSE, INFINITE); dwIndex -= WAIT_OBJECT_0; ...... // farther process at here } When the thread1 is terminated, the state of thread1 is set to signaled, then WaitForMultipleObjects() returns the index 0 of the array. But when the thread2 is terminated, the WaitForMultipleObjects() still returns the index 0 not index 1. I want WaitForMultipleObjects() to return the correct index, so I can process for this terminated thread. who can help me? Thanks a lot

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Jin Yao wrote: But when the thread2 is terminated, the WaitForMultipleObjects() still returns the index 0 not index 1. Are you sure that thread1 hasn't also terminated at the same time as thread2?

      J 1 Reply Last reply
      0
      • D David Crow

        Jin Yao wrote: But when the thread2 is terminated, the WaitForMultipleObjects() still returns the index 0 not index 1. Are you sure that thread1 hasn't also terminated at the same time as thread2?

        J Offline
        J Offline
        Jin Yao
        wrote on last edited by
        #3

        At the first time, thread1 terminates and WaitForMultipleObjects() returns the WAIT_OBJECT_0 + 0. The state of thread1 object is set to signaled. When the thread2 ends, the thread2 object will also be set to signaled. but at that time, the state of thread1 object is already signaled. So WaitForMultipleObjects() still returns the WAIT_OBJECT_0 + 0 not WAIT_OBJECT_0 + 1. I wish WaitForMultipleObjects() can return the WAIT_OBJECT_0 + 1 when thread2 ends. Thanks

        S 1 Reply Last reply
        0
        • J Jin Yao

          At the first time, thread1 terminates and WaitForMultipleObjects() returns the WAIT_OBJECT_0 + 0. The state of thread1 object is set to signaled. When the thread2 ends, the thread2 object will also be set to signaled. but at that time, the state of thread1 object is already signaled. So WaitForMultipleObjects() still returns the WAIT_OBJECT_0 + 0 not WAIT_OBJECT_0 + 1. I wish WaitForMultipleObjects() can return the WAIT_OBJECT_0 + 1 when thread2 ends. Thanks

          S Offline
          S Offline
          Scott H Settlemier
          wrote on last edited by
          #4

          when a thread in the handle array becomes signalled and the wait satisfied, handle the case and then remove that handle from the array (dec. the count, shift over remaining handles as required..) That handle will remain valid and signalled until you call CloseHandle on it, so you must remove it from the wait.

          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