Untitled

 avatar
unknown
csharp
2 years ago
578 B
1
Indexable
using System;
using System.Threading;

class Program {
    static void Main(string[] args) {
        int[] numbers = new int[] { 1, 2, 3, 4, 5 };
        int sum = 0;
        Thread[] threads = new Thread[numbers.Length];

        for (int i = 0; i < numbers.Length; i++) {
            threads[i] = new Thread(() => Interlocked.Add(ref sum, numbers[i]));
            threads[i].Start();
        }

        foreach (var thread in threads) {
            thread.Join();
        }

        Console.WriteLine("Sum: " + sum);
        Console.ReadLine();
    }
}