async/ I/O માટે તેનાથી આગળ, .NET પ્રદાન કરે છે માટે — એક્સિક્યુશન એક્સીધી બધી cores પર ચલાવવું. મુખ્ય તફાવત: / I/O concurrency માટે છે (non-blocking waits), જ્યારે , , અને PLINQ CPU-intensive કામ ને parallelize કરવા માટે છે.
async/ I/O માટે તેનાથી આગળ, .NET પ્રદાન કરે છે માટે — એક્સિક્યુશન એક્સીધી બધી cores પર ચલાવવું. મુખ્ય તફાવત: / I/O concurrency માટે છે (non-blocking waits), જ્યારે , , અને PLINQ CPU-intensive કામ ને parallelize કરવા માટે છે.
awaitasyncawaitTask.RunParallelasync/await → I/O-bound concurrency. Frees the thread during waits (DB, network).
Does NOT add CPU parallelism.
Parallelism → CPU-bound work spread across MULTIPLE CORES (computation, processing).
Use Task.Run, Parallel.For/ForEach, PLINQ.
// run a CPU-intensive computation on a thread pool thread (don't block the caller)
int result = await Task.Run(() => ExpensiveComputation());
// run multiple in parallel and combine
var tasks = items.Select(item => Task.Run(() => Process(item)));
var results = await Task.WhenAll(tasks); // wait for all
Task.Run thread pool thread પર કામ શેડ્યુલ કરે છે — તેનો ઉપયોગ CPU-bound computation ને parallelize કરવા માટે કરો (I/O માટે નહીં, જ્યાં async/await પહેલાથી જ પૂરતું છે વધારાના threads વિના).
// Parallel.For/ForEach — process a collection across cores
Parallel.ForEach(items, item => Process(item)); // automatically uses multiple cores
Parallel.For(0, 1000, i => Compute(i));
// PLINQ — parallel LINQ
var results = data.AsParallel().Where(x => IsValid(x)).Select(Transform).ToList();
Parallel.For/ForEach અને PLINQ (.AsParallel()) આપોઆપ કામ ને cores પર વહેંચે છે — CPU-heavy processing માટે બહુ સારું આધારિત આઇટમ્સ માટે. (માત્ર genuinely CPU-bound, સાઇઝમાં મોટા કામ માટે જ લાયક છે — overhead છે.)
// parallel code accessing SHARED state needs synchronization or thread-safe types
lock (_lock) { _counter++; } // lock for mutual exclusion
Interlocked.Increment(ref _counter); // lock-free atomic
var dict = new ConcurrentDictionary<string, int>(); // thread-safe collection
Tasks અને parallelism ને async/await ની બહાર સમજવું એ મહત્વપૂર્ણ senior-level જ્ઞાન છે કર્મક્ષમ C# applications બનાવવા માટે, અને I/O concurrency અને CPU parallelism વચ્ચે નિર્ણાયક તફાવત એ મુખ્ય દૃષ્ટિકોણ છે — સામાન્ય મૂંઝવણ બિંદુ. async/await I/O-bound concurrency સંભાળે છે (waits દરમિયાન threads મુક્ત કરે છે) પરંતુ CPU parallelism ઉમેરતું નથી, જ્યારે CPU-bound કામ (computations, data processing) વાસ્તવિક parallelism સુધી લાભ કરે છે cores પર via Task.Run (CPU કામ ને thread pool threads મા ઓફલોડ કરવું), Parallel.For/ForEach, અને PLINQ (.AsParallel()).
કયું સાધન યોગ્ય છે તે જાણવું — I/O માટે async/await, CPU-bound કામ માટે TPL parallelism સાધનો — અસરક્ષમ performance optimization માટે આવશ્યક છે, કેમકે ખોટું એક ઉપયોગ કરવું (ઉદાહરણ તરીકે computation ને ઝડપ આપવાની અપેક્ષા async સાથે, અથવા I/O માટે threads શરૂ કરવું કે જે async કાર્યક્ષમતાથી સંભાળે છે) અમતર છે.
સમાન મહત્વપૂર્ણ છે thread safety સમજવું: parallel કોડ shared state ને access કરતું વર્તમાન ત્રણ સુધી ધ્યાન આવશ્યક છે (lock, Interlocked atomic operations માટે, અથવા thread-safe collections જેમ ConcurrentDictionary) race conditions ટાળવા માટે — parallel કોડ માં નિર્ણાયક correctness મુશ્કેલ.
I/O-vs-CPU તફાવત, parallelism સાધનો (Task.Run, Parallel, PLINQ), અને thread-safety આવશ્યકતાઓ સમજવું યોગ્ય છે સાચો, કર્મક્ષમ concurrent અને parallel C# લખવા માટે.
આધુનિક applications ને ઘણીવાર I/O concurrency અને CPU parallelism બંને જરૂર હોય છે, અને યોગ્ય અભિગમ પસંદ કરવા અને thread safety યોગ્ય રીતે સંભાળવું એ જે parallel કોડ અસર અને સાચી બનાવે છે, તે મહત્વપૂર્ણ, વારંવાર-સંબંધિત senior જ્ઞાન છે performance-sensitive C# development માટે.