I have a large, dynamically generated tree, in which I am interested in only the leaves. As the generation of new nodes only depend on the parent node, I want to do as much as possible in parallel, so here is my plan:
- Starting up
n
threads - Each thread takes one node from the top of a ConcurrentStack
- Each thread generates the child nodes and puts them on the ConcurrentStack, unless it is a leave (has reached the maximum depth of the tree)
- Going back to step 2, until the stack is empty, and all threads are waiting. Then the thread should end.
Well, as I know the depth and branching factor of the tree, it is possible to calculate the number of nodes in the tree and so I want to display a progress bar.
To do this, I thought about using a common static variable, that I lock each time, increment it, unlock it again. In the display thread I think I don't need to lock it, as it is not critical to have the latest number and I don't base calculations on it. But somehow I have a bad feeling about it. Is my plan safe? I am quite new to parallel computing.
New Approach I know how many Threads are running, and just approximate the number of processed nodes by multiplying the thread number with the count of nodes one of those threads processed. The result wouldn't be accurate, but maybe accurate enough, and it would safe me from locking a common variable.
My main focus lies on monitoring the progress and counting the processed nodes, though I appreciate all comments that help to improve my knowledge and skills regarding parallel computation.
Thanks in advance, -M-