Concurrency and Source Panels
Thread Result Slots
Run two named thread methods and collect their results in stable slots.
Thread Result Slots
ThreadResultSlots.cs
using System;
using System.Threading;
class Program
{
static int baseScore;
static int leftResult;
static int rightResult;
static void LeftWorker()
{
int adjusted = baseScore + 2;
leftResult = adjusted;
}
static void RightWorker()
{
int adjusted = baseScore * 2;
rightResult = adjusted;
}
static void Main()
{
int seed = ;
baseScore = seed;
Thread left = new Thread(LeftWorker);
Thread right = new Thread(RightWorker);
left.Start();
right.Start();
left.Join();
right.Join();
int total = leftResult + rightResult;
Console.WriteLine($"seed={seed}");
Console.WriteLine($"total={total}");
}
}
using System;
using System.Threading;
class Program
{
static int baseScore;
static int leftResult;
static int rightResult;
static void LeftWorker()
{
int adjusted = baseScore + 2;
leftResult = adjusted;
}
static void RightWorker()
{
int adjusted = baseScore * 2;
rightResult = adjusted;
}
static void Main()
{
int seed = ;
baseScore = seed;
Thread left = new Thread(LeftWorker);
Thread right = new Thread(RightWorker);
left.Start();
right.Start();
left.Join();
right.Join();
int total = leftResult + rightResult;
Console.WriteLine($"seed={seed}");
Console.WriteLine($"total={total}");
}
}
using System;
using System.Threading;
class Program
{
static int baseScore;
static int leftResult;
static int rightResult;
static void LeftWorker()
{
int adjusted = baseScore + 2;
leftResult = adjusted;
}
static void RightWorker()
{
int adjusted = baseScore * 2;
rightResult = adjusted;
}
static void Main()
{
int seed = ;
baseScore = seed;
Thread left = new Thread(LeftWorker);
Thread right = new Thread(RightWorker);
left.Start();
right.Start();
left.Join();
right.Join();
int total = leftResult + rightResult;
Console.WriteLine($"seed={seed}");
Console.WriteLine($"total={total}");
}
}
thread
Threads may finish in either order, so shared examples should write to stable positions before the main thread joins and reads them.