Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Why don't you try to recycle the threads, since creation/destruction is expensive? </p> <p>Code a thread pool class and send tasks to it. The pool will either queue the tasks if it has no more available threads, create threads if current_threads &lt; max_threads or just use what thread is available.</p> <p>Suggested implementation:</p> <p>Find out what your ideal thread count is. This is usually equal to the number of processors. Depending on how complicated you want this to be, you could create all the threads in the pool at once or add threads if current-thread-count &lt; ideal-thread-count and all the existing threads are busy executing tasks.</p> <p>Assuming that you are creating all your threads at once, you need to pass a worker function to each of the threads to execute. This worker function will wait for tasks to become available and then execute them. Because the function either executes a task or waits for it, it won't return and <em>the thread won't be destroyed</em>.</p> <p>The thread pool can keep track of a task queue and manage a wait condition that indicates when there are tasks available in the queue. Each thread worker function waits on the wait condition and when there's a task available it wakes up and tries to do the task. You will have to do some synchronization; the easiest way would be to try and find an available thread pool implementation, like the one in Windows (Vista+ I think) or the one in QtConcurrent which would allow you to just pass the task, call run and let the OS/library worry about everything.</p> <p>Later edit: </p> <p>Check out <a href="http://threadpool.sourceforge.net/" rel="nofollow noreferrer">http://threadpool.sourceforge.net/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload