سؤال

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?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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

You should do it like this

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

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

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top