Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4608804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:53:49+00:00 2026-05-22T00:53:49+00:00

I want to test out using WaitForMultipleObjects, and to do so I want to

  • 0

I want to test out using WaitForMultipleObjects, and to do so I want to start multiple threads, using a loop, each passing different ThreadArgs.

(Essentially just an array of five ThreadArgs, and five HANDLEs?)

When I try to create an array of either the struct, or the HANDLE, neither will work and I get the error ‘cannot allocate an array of constant size 0′ for both, and ”initializing’ : cannot convert from ‘HANDLE’ to ‘HANDLE []” for the latter.

Is an array the appropriate way to do this with regards to the struct? (Also, a note – it will have to remain a struct as it will contain six members eventually, I’m just trying to get it working in a simpler form at first, since adding these members should be very straightforward)

And I’d assume an array of Handles is the best way to do this, but how do I go about declaring one?

Thank you!

#include <windows.h>
#include <iostream> 
#include <process.h>


struct ThreadArgs 
{
    int id;
};

ThreadArgs args = {1}; 


unsigned int __stdcall MyThread(void *data)
{
    std::cout << "Hello World!\n"; 
    ThreadArgs *args = (ThreadArgs *) data; 

    std::cout << (*args).id;

    return 2;
}

int main()
{

    HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, &args, 0, NULL); 

    WaitForSingleObject(hThread, INFINITE);


    while(true);

}

The above is my code currently.

I was trying to create an array of HANDLEs using –

HANDLE hThread[5]; 

Edit:

The error is on this line, when it is altered to be an array of HANDLE[5] –

HANDLE hThread[0] = (HANDLE) _beginthreadex(NULL, 0, MyThread, &args, 0, NULL); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:53:50+00:00Added an answer on May 22, 2026 at 12:53 am

    You need to use WaitForMultipleObjects and pass it an array of handles to wait for multiple threads to complete. See an example here.

    #include <windows.h>
    #include <iostream>
    #include <vector>
    using std::vector;
    using std::cout;
    using std::endl;
    
    CRITICAL_SECTION cs;
    
    struct ThreadArgs 
    {
        int id;
    };
    
    VOID MyThread(void *data)
    {
        EnterCriticalSection(&cs);
        ThreadArgs *args = (ThreadArgs*)data;
        cout << args->id << endl;
        LeaveCriticalSection(&cs);
    }
    
    int main()
    {
        InitializeCriticalSection(&cs);
    
        vector <HANDLE> T;
    
        DWORD id;
    
        ThreadArgs args[5] = {1, 2, 3, 4, 5};
    
        for (int i = 0; i < 5; i++)
        {
            T.push_back(CreateThread(0, 0, (LPTHREAD_START_ROUTINE)MyThread, &args[i], 0, &id));
        }
    
        WaitForMultipleObjects(5, &T[0], TRUE, INFINITE);
    
        DeleteCriticalSection(&cs);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to test ASP.NET application using NUnit, but it seems WebConfigurationManager.ConnectionStrings collection is
I want to test my software on different Windows Operating Systems. I plan to
I am attempting to write some tests using webtest to test out my python
I'm using selenium-client to test a site. I want to get all a tags
I want to test my rails web service using cURL, yet so far I
We want test the JTAPI feature of our application. Are there any emulator for
I want to test the behavior of a certain piece of .NET code in
I want to test the web pages I create in all the modern versions
I want to test ASP.NET applications to get the feel for the MVC extension
I want to test some PHP on my local machine running Windows XP Professional.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.