My multi-threaded program works, but I find that running with several cores actually takes longer than does a single-thread run. Here's what is going on:
1. Several threads are started. They each go into a DO WHILE loop, waiting for a flag in an indexed variable in a named COMMON block.
2. When the flag is set, the thread starts some serious number crunching, using data taken from another indexed set of variables. When the routine finishes, it sets another flag, also in an indexed array.
3. The calling program waits until all threads have finished, checking the flags in another DO WHILE loop, and then reads out the answers from the indexed variables. Then it starts over with another set of input data, and so on.
This should run faster with several threads running in parallel, but it doesn't. Possible reasons:
1. One of the threads is in a core being used by Windows for something else. It cannot complete its task until Windows lets it run, and the calling program has to wait for that.
2. The several threads do not in fact run in parallel. If they go individually, that would also be very slow.
Is there any way to ensure that my threads get their own cores, and they all run in parallel?