EDIT: To clarify and simplify: I'm looking for a "good" way to submit more Stackable objects to a Pool whenever a Stackable ends (using data from this first Stackable to add the second one). I have the ideas of polling the objects until one ends (inefficient and ugly) and passing references to Pool object (I was not able to make it work). The base code is this one: https://github.com/krakjoe/pthreads/blob/master/examples/Pooling.php
Now, the full description:
I'm working on an application in PHP, that has grown too much and takes a lot of time. Because of this I'm trying to multithread that application, using a thread pool (I know PHP is not the best option, but I don't want, and can't change the language at this point).
The problem is, there are 2 stages of the application, that have to go in order, and each one has a lot of subtasks that can go concurrently. So, this is the process in my head:
- There will be N subtasks in stage 1, these subtasks will be Stackable objects.
- When subtask i ends, the "main" (the one that creates the pool, the Stackables, etc.) has to be notified, and execute stage 2 for subtask i with some data from the subtask i (a different Stackable object). In this stage, there will be M subtasks for each of the subtasks of stage 1.
I would like to use the same thread pool for threads in stage 1 and stage 2, and the only solution I can thing of for going from stage 1 to stage 2 is poll each of the N subtasks until one of them ends, and then call stage 2 for the one that ended, and repeat until all the N subtasks end.
I'm using the example of a thread pool included in pthreads source, by Joe Watkins, as a base code.