When i use AsParallel(), the next operations runs in multi-thread, so, the methods that i use in this query, should be a thread safe?
In the next example, the method Convert(string value) is not thread-safe but is being used in a Select() that run as AsParallel().
Is it correct to use AsParallel() like standar Linq?
Note: The Convert() method it's just an example, but imagine a more complex method that really required a thread-safe implementation, when it will be executed in a multi-thread environment.
List<string> myNamesList = new List<string>()
{
//Initialize with a lots of elements
}
List<string> myConvertedNameList = myNamesList.AsParallel()
.Where("Any Condition")
.Select(x => Convert(x))
.ToList();
public string Convert(string value)
{
int length = value.Length;
string myFantasticValueConverted = "_" + value + "["+ length +"]";
return myFantasticValueConverted;
}
Is it necessary to use thread-safe methods when I use AsParallel?
Yes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With