Threadpool local variable concepts
I am trying to multi thread my application using ThreadPool
in c#. I have a 3D array in which I have to process for every row separately. I have spawned out threads equal to number of processors(cores) in the system and then dividing the task between the threads to process on the individual rows.
A single function is called for all the threads in which I am using the thread to divide the data between different threads. (Kind of SIMD thing). The function calls other small functions as well. Also the functions create d开发者_StackOverflow社区ynamically allocated arrays as a temporary storage space for intermediate values. I want to know what is the concept of local variables in threads. Does calling a single function in multiple threads make local copies of the variables in individual threads? How do I go about designing such a code?
Please explain..
Does calling a single function in multiple threads make local copies of the variables in individual threads??
Well, it's not that it makes local copies of the variables. Every local variable / thread pair is a specific storage location, and that storage location is not the same for any other local variable / thread pair.
I have spawned out threads equal to no of processors(cores) in the system and then dividing the task between the threads to process on the individual rows.
Why are you doing this yourself? Use the TPL.
I think you should rethink the solution and try to use Parallel.For for your solution.
MSDN Parallel.For
But to answer your question you can use a single function in multiple threads if it is designed to be thread safe. And you can use local variables in your functions because they are local to the function and every call will get a separate memory location.
精彩评论