Note that there are some explanatory texts on larger screens.

plurals
  1. POboost::thread and starting or not starting large numbers of threads at the same time
    primarykey
    data
    text
    <p>In researching my question on this forum, I have found my same basic question about boost::thread asked a number of times before, but none of the answers seem to answer my specific needs. The thrust of the question from the way I'm thinking about it is how to instantiate a number of threads without actually launching any of them until you are ready. The answer always seems to be in the vein of why not just wait until you are ready to launch each thread before instantiating it. Indeed, it appears to me that's the way boost::thread is designed to work. That is, it appears that instantiating a boost::thread object seems to want also to launch the thread, and it's not clear to me if these to functions can really be separated.</p> <p>I'll try to explain a little more about my application than some of the other questioners have explained. I need to launch at least dozens if not hundreds or even thousands of threads. Each thread will run the exact same function, and the threads will be differentiated from each other by a single and unique argument that is passed to the function as a part of each thread. Each thread will use large amounts of shared memory that will be read only and that does not need to be synchronized. Each thread also will use a substantial amount of local memory that will be allocated dynamically and which will be released prior to the completion of the thread. The local memory also does not need to be synchronized. Indeed, I'm thinking I may be able to get by without any synchronization between the threads at all except joins to await the completion of each thread.</p> <p>The program will run for dozens or even hundreds or possibly thousands of hours, so I want to use all the processor cores on my machine, but I don't want to initiate more threads at the same time than the number of processor cores that I have available. The problem is that if I were to initiate too many threads at the same time, they would quickly exhaust my machine's memory because of the local memory used by each thread. But if I can manage to initiate only as many threads as I have processor cores, then the memory situation will be fine.</p> <p>So if I have n cores, I want to launch n threads and wait until one of them is done. When one of then is done, I want to launch one more, etc. until all the work is done. I have looked at thread_group and join_all. On the thread_group itself, I can't figure out how to wait until one of the first n threads is completed before instantiating the (n+1)st one, etc. And join_all is not what I really need because I just want to wait until less than n threads are still running, and it shouldn't matter which one finishes first. Whenever the number of active threads becomes less than n, I want to launch a new one. I'm quite sure that add_thread() is going to be involved in this somehow or other. I'm also quite sure that the boost::thread size() function is going to be involved somehow or other. But I can't really see how to put all the pieces together. And the problem always seems to come down to not quite being able to understand how to instantiate a thread only when I'm ready. And for example, all the boost::thread examples I have been able to find seem to have "statically named" thread objects, viz., "boost::thread MyWorkerFunction" etc. that get instantiated as they are encountered in the code. Well, I could have thousands of threads for the same MyWorkerFunction and I only want to dispatch n of the threads at the same time, and I don't understand how to do that.</p> <p>Thanks, Jerry</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload