4
votes

I have an std::vector of handle objects. I have to wait on these handle objects for using in the WaitForMultipleObjects function. Since it's a vector, I am getting an error while using it in WaitForMultipleObjects:

std::vector<HANDLE> events;
// ...
WaitForMultipleObjects(events.size(), events, true, INFINITE);

Is there any way to do this?

4
As @Joachim said, the second parameter should be a pointer, so why not send &events[0] instead of events?Eitan T

4 Answers

4
votes

Preferably, if you've got an up to date version of STL, you should use:

WaitForMultipleObjects(events.size(), events.data(), true, INFINITE);

With older STL's, you can use &events[0] if .data() isn't available as a method on vector.

1
votes

If you look at the documentation for WaitForMultipleObject you will see that the second argument is a pointer, not a std::vector. The std::vector class can not be used instead of a pointer or native array.

The onlyOne way you can do is to create a temporary "array", and copy all the handles to it, and use that as argument.

Another way, as suggested by Charles, is to use &vector[0] or as suggested by Tony to use vector.data() (if available).

1
votes

You should do it like this

WaitForMultipleObjects(events.size(), &events[0], true, INFINITE);

This is portable and the-way-to-do-it.

1
votes

The canonical way to get to the underlying vector buffer is

&events[0]

So you can do this:

WaitForMultipleObjects(events.size(), &events[0], true, INFINITE);

Also see this similar question.