|
-
February 5th, 2012, 08:05 AM
#1
Understanding multithreading
I'd like to add multithreading to an application I'm developing, so I'm reading may tutorials and articles online about that.
I wrote have a function that:
1. reads the data from an array;
2. processes the data in a "for" cycle (that's where 70% of the time is spent) and saves the processed data for each iteration of the cycle in another array;
3. does some more stuff with the processedData array.
I think that on a multiprocessor machine, dividing operation (2) in more threads, each one analyzing a different section of the array, would improve performance of the function.
I read online that multithreading poses some code safety risks when accessing the same memory location. In my function I want all the threads to access the same public array to read data (possibly from the same location - say dataToProcess[i,j] where i and j are calculated by using sin and cos of an angle) and write to another public array (never to the same item - processedData[k] where k is a number of the section being analyzed).
Can I do that or as I have to read data from the same location can generate problems?
Another question I have is related to operation (3). I want to wait for all the threads to be done before continuing. The Join method seems to be what I'm looking for, but I'm not sure how to use it. If I create more than one thread, do I have to call "Join" for all the threads in the main function? If I do that does the main function wait for all the threads to finish and then continue to the following instructions?
Thanks for helping me!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|