Home > Back-end >  Is it a bad thing to use an array of threads?
Is it a bad thing to use an array of threads?

Time:07-30

Let's say that I have to do the same operation on thousands of data, my idea was:

  1. Create an array of suspended TThreads (pass the data on creation)
  2. Get the CPU core count
  3. Start core count threads then wait until all are done
  4. Repeat 3 until all the data is processed
  5. Get the results
  6. Free the threads and the array.

Is it okay to do it like this?

CodePudding user response:

Creating thousands of threads is a bad idea. Why?

Creating each thread instance consumes some memory. So creating thousands of them could consume quite some memory

Based on answer in Maximum threads limit per process in windows 10? a 32 bit application might be able to create just about 2000 threads before running out of memory. That is on condition that such applications is not using memory for anything else.


Best approach is to create just as many threads as there are cores available on your computer and then split the work equally between them.

Or you could go and assign work to these threads in smaller chunks. This allows to balance the work in case when some of the threads might get assigned more cpu time and therefore finish their work faster.

CodePudding user response:

Create an array of suspended TThreads (pass the data on creation)

I would put the data into a queue instead, and then create only as many threads as there are cores, or the number of items in the queue, whichever is lesser. Let each thread then pull from the queue until there is nothing left to pull.

Start core count threads then wait until all are done

When any given thread is finished with a queue item, it can notify the rest of the code if needed (if you need to grab the result in real time) and just pull the next item from the queue. That way all of the threads are running at their max capacity. Once the queue is empty, you will have all of the results, and can either suspend the threads for future reuse, or destroy them.

  • Related