Multithreaded loop?!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Guy007
    New Member
    • Mar 2007
    • 24

    Multithreaded loop?!

    Hi guys!

    I have a simple for loop in c# that traverses all elements in an array, and invokes a method ProcessArrayInd ex() each time. The result is stored in another array, at the same index.

    Code:
    int[] processedArray;
    int[] myArray;
    
    for (int k = 0; k < myArray.Length; k++)
    {
      myArray[k] = ProcessArrayIndex(k);
    }
    Now I would like to make this faster, through the use of multiple threads.

    I would like to have (for example) 10 threads working in parallel. Each loop iteration grabs one of the free threads for it to execute. If all threads are busy, the loop will wait until a thread becomes available. I tried messing something up with ThreadPool but I didn't manage to get what i want.

    Basically I would need:
    -> A way to have multiple loop iterations running concurrently (with a limited maximum number of threads)
    -> A way to make sure that all threads have finished executing before continuing with the rest of my code!

    Any ideas &/or sample code would be very much appreciated!

    thanks!
  • Guy007
    New Member
    • Mar 2007
    • 24

    #2
    BTW, the size of the loop is not known. I might have even a couple of hundred elements to process... :S

    Comment

    • nmadct
      Recognized Expert New Member
      • Jan 2007
      • 83

      #3
      First of all, be aware that splitting the job up into 10 threads won't make things any faster unless you're running the program on a machine with many CPUs. On a single-CPU machine, you will probably lose speed by using multiple threads. That said, let's look at how you would go about it.

      I would have each thread process a subsection of the range of indexes. That way you don't have to deal with any kind of thread pooling, instead you have a fixed number of threads but the number of indexes that each thread is responsible for processing increases as the overall input range gets larger.

      Each thread generates a list of results. Then, when the threads are all done, concatenate the result lists in the proper order (i.e. the same order as their inputs).

      Comment

      • nmadct
        Recognized Expert New Member
        • Jan 2007
        • 83

        #4
        If the time it takes to process each index is highly variable, so that threads with the same number of indexes to process are likely to finish at different times, you could make all of the threads take indexes from a shared counter or queue. The problem with this method is that you have to do locking on the queue or counter, which introduces a bottleneck. Ideally you want threads to run independently with as little communication as possible for as long as possible to get maximum benefit from multithreading.
        Last edited by nmadct; Apr 7 '07, 10:58 PM. Reason: typo

        Comment

        Working...