4

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?

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
Aneesh Narayanan
  • 3,220
  • 11
  • 31
  • 48
  • As @Joachim said, the second parameter should be a pointer, so why not send `&events[0]` instead of `events`? – Eitan T Jun 19 '12 at 08:27

4 Answers4

4

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.

Community
  • 1
  • 1
Scott Langham
  • 58,735
  • 39
  • 131
  • 204
1

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).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

You should do it like this

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

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

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
1

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.

Community
  • 1
  • 1
sharptooth
  • 167,383
  • 100
  • 513
  • 979