wait for unknown number of threads created by CreateThread

Bit Manipulator

I need to create an unknown number of threads and at the end want to wait for all the created threads to finish their jobs. After searching, I could only get to wait for some known number of created threads using WaitForMultipleObjects(. . .) which expects pointer to an array of handles of created threads. The following code is an example of the scenario.

#include "stdafx.h"
#include <Windows.h>
#include <time.h>

FILE* fp;
time_t start;

struct mydata{
    int a, b;
};

void myFn(mydata* p)
{
    fprintf(fp, "a = %d\n", p->a);
    fprintf(fp, "b = %d\n", p->b);
    delete p;
}

bool condition()
{
     if(time(0) - start >= 1)
        return false;
     return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
    start = time(0);
    fopen_s(&fp, "outOfExe.txt", "w");
    mydata* dataToGive;
    int n = 0;
    while(condition())
    {
        n++;
        dataToGive = new mydata;
        dataToGive->a = 2*n;
        dataToGive->b = 4*n;
        HANDLE WINAPI handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)myFn, dataToGive, 0, NULL);
    }
}

The solution I could think of is to create an array of size equal to number of cores in CPU and at any given time execute at max of this number of threads. But for this I would require to develop a queuing mechanism etc. Is there some way to wait for all the created threads by the way shown in above example?

Martin James

int threadCount, a critical section, an event and WaitForSingleObject?

Lock the CS, create your threads, incing the threadCount, then unlock the CS and wait on the event.

In the thread function/method, do your stuff then, at the end, lock the CS, dec the threadCount. If it is then 0, signal the event. Unlock the CS and exit the thread.

No array, no vector, no, (sane), limit on the number of threads.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Wait for an unknown number of futures

From Dev

Wait for an unknown number of futures

From Dev

How to limit number of threads created and wait main thread until any one thread finds answer?

From Dev

How to wait for all threads created within a method

From Dev

How to wait for threads to finish their work, where threads created by clone in c?

From Dev

How to limit maximum number of threads created and wait main thread until any one thread finds answer, before exiting function?

From Dev

Limit the number of threads and wait if max threads reached using @Async annotation

From Dev

Combine/merge an unknown number of observables together as they are created

From Dev

Combine/merge an unknown number of observables together as they are created

From Dev

Unit Test for number of threads created by threading code

From Dev

What is limiting the number of threads created in a Java application?

From Dev

how TPL decide the number of threads to be created

From Dev

large number of threads is wait state in threadpool causing performance problems

From Dev

large number of threads is wait state in threadpool causing performance problems

From Dev

Adding wait time in jmeter after every n number of threads

From Dev

Number of threads that can be created in Java in 32 bits machines

From Dev

Set Max Number of Created Threads for Java application using Linux

From Dev

What is the max number of async threads created for kafkatemplate async response

From Dev

Determining the number of threads created by Executors.newSingleThreadExecutor().execute?

From Dev

How to know the number of Threads created and limit the Tasks accordingly

From Dev

C# .Net - How to make application wait until all threads created in Library are finished

From Dev

Wait for all Threads

From Dev

Threads with no wait in Java

From Dev

Wait for all Threads

From Dev

How to wait for a specific amount of time after creating a specific number of tasks\Threads?

From Dev

Wait for a file to be created with a timeout

From Dev

How can I get the maximum number of OpenMP threads that may be created during the whole execution of the program?

From Dev

Main thread wait for other threads

From Dev

Wait multiple threads for one result

Related Related

  1. 1

    Wait for an unknown number of futures

  2. 2

    Wait for an unknown number of futures

  3. 3

    How to limit number of threads created and wait main thread until any one thread finds answer?

  4. 4

    How to wait for all threads created within a method

  5. 5

    How to wait for threads to finish their work, where threads created by clone in c?

  6. 6

    How to limit maximum number of threads created and wait main thread until any one thread finds answer, before exiting function?

  7. 7

    Limit the number of threads and wait if max threads reached using @Async annotation

  8. 8

    Combine/merge an unknown number of observables together as they are created

  9. 9

    Combine/merge an unknown number of observables together as they are created

  10. 10

    Unit Test for number of threads created by threading code

  11. 11

    What is limiting the number of threads created in a Java application?

  12. 12

    how TPL decide the number of threads to be created

  13. 13

    large number of threads is wait state in threadpool causing performance problems

  14. 14

    large number of threads is wait state in threadpool causing performance problems

  15. 15

    Adding wait time in jmeter after every n number of threads

  16. 16

    Number of threads that can be created in Java in 32 bits machines

  17. 17

    Set Max Number of Created Threads for Java application using Linux

  18. 18

    What is the max number of async threads created for kafkatemplate async response

  19. 19

    Determining the number of threads created by Executors.newSingleThreadExecutor().execute?

  20. 20

    How to know the number of Threads created and limit the Tasks accordingly

  21. 21

    C# .Net - How to make application wait until all threads created in Library are finished

  22. 22

    Wait for all Threads

  23. 23

    Threads with no wait in Java

  24. 24

    Wait for all Threads

  25. 25

    How to wait for a specific amount of time after creating a specific number of tasks\Threads?

  26. 26

    Wait for a file to be created with a timeout

  27. 27

    How can I get the maximum number of OpenMP threads that may be created during the whole execution of the program?

  28. 28

    Main thread wait for other threads

  29. 29

    Wait multiple threads for one result

HotTag

Archive