Jump to content

Hi.

I have this struct array:

struct RunThread {
        HANDLE Thread;
        bool FirstTime = true;
        std::atomic<bool> Running = false;
        DWORD ThreadID;
    }ThreadList[100];

 

I want to use the Thread Handles in this in WaitForMultipleObjects.

 

I tried this:

DWORD result = WaitForMultipleObjects(100, &ThreadList->Thread, FALSE, INFINITE);

For Which I Got WAIT_FAILED with a GetLastError of 87

Quote

 

ERROR_INVALID_PARAMETER

87 (0x57)

The parameter is incorrect.

 

 

Is it my syntax that is incorrect or is doing something like this straight up impossible?

I'm sure I can just create an array of references to these (since it'd be erased after WaitForMultipleObjects returned anyway), but I'm not too comfortable with that, it sounds nasty.

fuck.png

(Not so sure anymore lol)

 

Creating the whole struct again to have the array inside RunThread instead of being an array of RunThread seems like a good solution but I'd rather have it working as is if there aren't any major disadvantages to doing so.

 

Is there a way to do this? if not, how should I go about it? Thank you.

Link to post
Share on other sites

The problem is WaitForMultipleObjects expects a pointer to the first element in a array of HANDLE's. The HANDLE's should be next to each other in memory as they are in an array. In your current code the HANDLE's have all those other struct members and possible padding in between.

 

An array of references is not allowed in C++ and would not work anyway for the same reason a array of pointers won't work. References or pointers merely refer to the actual object, while WaitForMultipleObjects expects an array of actual elements to work with.

 

Possible solutions:

-Refactor your code so the HANDLE's are kept in a array of their own, struct RunThread could still hold a reference to a HANDLE in the array so your existing code can continue to work.

-In stead of WaitForMultipleObjects, use WaitForSingleObject in a loop and pass 1 HANDLE at a time.

Link to post
Share on other sites

3 minutes ago, Unimportant said:

The problem is WaitForMultipleObjects expects a pointer to the first element in a array of HANDLE's. The HANDLE's should be next to each other in memory as they are in an array. In your current code the HANDLE's have all those other struct members and possible padding in between.

 

An array of references is not allowed in C++ and would not work anyway for the same reason a array of pointers won't work. References or pointers merely refer to the actual object, while WaitForMultipleObjects expects an array of actual elements to work with.

 

Possible solutions:

-Refactor your code so the HANDLE's are kept in a array of their own, struct RunThread could still hold a reference to a HANDLE in the array so your existing code can continue to work.

-In stead of WaitForMultipleObjects, use WaitForSingleObject in a loop and pass 1 HANDLE at a time.

Thanks a lot.

I'll probably go for the first one since I need to know when any of the 100 threads turns to signaled and I don't think that's achievable with the 2nd option.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×