Answer the question
In order to leave comments, you need to log in
How to limit the number of executable threads?
There are n jobs in the queue, each running on a separate thread. You need to make sure that only 2 threads are running at the same time. For example, two threads, one of them has ended, you need to start another thread from main.
Answer the question
In order to leave comments, you need to log in
Make a job queue.
You make a pool of N threads (in your case 2), run these threads.
Each thread waits for a new job to arrive, picks it up from the queue, and executes it.
At the end of the work, the thread does not terminate, but waits for the next task to arrive.
Jobs are queued and taken out of the queue with the blocking of this queue.
Threads in the pool are terminated only when the application terminates.
If the thread is attached, then in main you can wait for it using thread join, if not attached - set some flag in the thread just before the end, which means the end of the thread. In main, check the flag.
But it's more "technological" to make it so that when the thread has completed one task, it again climbed into the queue and took another task from there. If there are no tasks, then let him wait. Synchronization primitives such as mutexes and the like will help you with this.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question