Arrays and Threads
-
In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?
-
In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?
AFAI understand it, you need to create a syncronized version of the collection/set , that would allow many threads to read, but only one to write. I dont think u need to lock the entire array though, isnt it just possible to lock the element? leppie::AllocCPArticle(Generic DFA State Machine for .NET);
-
In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?
You're probably best off working with a collection, as .NET collections have built-in synchronization features. Look it up on MSDN.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?
Have a look at
ArrayList.Syncronized(IList)
leppie::AllocCPArticle(Generic DFA State Machine for .NET); -
In C you could have multiple threads simultaneously writing to the same array but different array indexes without worrying about race conditions. Can I do the same thing in C#? I ask because an array in C# is more than just a chunk of memory. Do I need to lock the entire array before I write to a single element even if I know that each thread will be writing to a different index?
albean wrote: I ask because an array in C# is more than just a chunk of memory. Not exactly. One-dimension arrays in C# are actually objects that store a pointer to a C-style array, so what you are asking is actually safe on C#, if your algorithm is really thread safe. ORACLE One Real A$#h%le Called Lary Ellison
-
albean wrote: I ask because an array in C# is more than just a chunk of memory. Not exactly. One-dimension arrays in C# are actually objects that store a pointer to a C-style array, so what you are asking is actually safe on C#, if your algorithm is really thread safe. ORACLE One Real A$#h%le Called Lary Ellison