I understand the essence of the pool, but did not find a concrete implementation. Those. I wonder how the pool can reuse the threads if they have completed with 1 task and accept a new one, but as far as I know the stream cannot be reused after the task is completed, i.e. re-execute Thread.start () cannot be used, hence the question of how a thread pool saves resources on creating a thread, if the threads cannot be reused after completion and you still need to re-create the stream, i.e. I'm interested in the mechanism. Maybe I do not know something or missed something and just do not know how to reuse the stream without receiving an Exception. I would also like to hear the opinion of C ++ specialists (I am not very strong in C ++), if there are differences with Java. Thank you in advance.
- Download the boost threadspool source code and (and it’s better to start, because the code is much clearer) Poco, and look at the implementation. This is the best option - to see how it is solved by professionals whose code is used by millions. - Arkady
|
1 answer
And who told you that the stream ends?
If you simplify, then the threads in the pool spin endlessly, in fact, it is:
while (true){ // берётся задача из очереди и выполняется sleep(...); } Closer to reality is something like this:
while (!pool.isShutdown()) { Runnable task = pool.waitForTaskOnQueue(); // ждём задачу task.run(); // выполняем } - oneI understood, in principle, I imagined it this way, but did not think that everything was so trivial. It seemed to me that there were some “Magic Functions” there that I did not know about :)) - BORSHEVIK
- Add to the cycle the pending work execution, and it seems that he spends resources when he waits. Anything like pthread_cond or semaphores - Mike
- @Mike is a matter of course) - Suvitruf ♦
- @BORSHEVIK actual implementation may vary. But one thing is for sure - the flows do not end there, for this would not make sense. Creating a stream is a pretty expensive business. Therefore, it is easier to create them once and just put in waiting. - Suvitruf ♦
- @Suvitrus , I only guessed that the author wrote the answer, but did not think that everything is so simple. - BORSHEVIK
|