2

I have a vector<HANDLE>. I want to wait for all of them to finish. I don't want to copy them over to an array. What are the benefits of doing so anyway and using WaitForMultpleObjects, rather than using WaitForSingleObject in a loop if any?

Thanks!

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    Off the top of my head, the `WaitForMultipleObjects` function allows you to wait for **all** of the objects to finish, whereas `WaitForSingleObject` only waits for a single object to finish. Which one you use depends on which behavior you require. Is there more to the question than this? – Cody Gray - on strike Dec 18 '11 at 05:09
  • With `WaitForMultpleObjects()` you can also observe events happening one by one. With `WaitForSingleObject()` you can't always observe events in the order of occurrence, unless, the timeout is set to the minimum, which would make the use of `WaitFor*Object*()` pointless because of effectively degrading it to polling. – Alexey Frunze Dec 18 '11 at 05:35

1 Answers1

2

First, in every C++ compiler I know of, vectors are internally an array, and you can get a pointer to the array with &yourvector[0]. In C++11 I believe this behavior is also required by the standard. So there's no need to copy.

As for the benefit, WaitForMultipleObjects will reduce the number of times you wake up, saving some small amount of CPU time. It will also atomically acquire all of the HANDLEs at the same time - if you have a bunch of mutex or semaphore objects and need to lock them all, this can be important.

bdonlan
  • 224,562
  • 31
  • 268
  • 324