OK, first create your random stream. Since thread synchronization is relatively expensive compared to generating one random, loading a vector (say with 10k power) using randoms (as suggested by Yang) is a good idea. Creating, terminating, and destroying threads is also a PITA, so the "random" loop around waiting on the "go" AutoResetEvent (see MSDN), initialized to true, the stream then generates a random vector at startup, and then is signaled when "go".
You need a mechanism for wiat until the vector is fully assembled before taking responsibility for it. You can place it in the consumer-producer queue, perhaps in the Windows message queue, as Ian suggested, but it may be easier (in this case) to simply take the vector from the stream when this is done. You can use another "full" AutoResetEvent initialized to false and wait on it for a random thread that signals this when it is done.
As soon as you take the vector, report the 'go' event to trigger a random thread that generates another vector so that it is probably already completed when you need it later.
You need a vector copy in which ownership can be easily transferred. I would probably just use a pointer, creating one with a new one in a random thread, generating randoms, copying the pointer value in the main thread and deleting it when that is done. A random thread will only be a new different vector whenever it passes "go", so it resets its own pointer. If you have a suitable smart_ptr class, you can use it, possibly unique_ptr, since it can be moved.
source share