Skip to content

Instantly share code, notes, and snippets.

@retran
Created May 21, 2019 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retran/0f47c5c239ddef876e3674b641da7574 to your computer and use it in GitHub Desktop.
Save retran/0f47c5c239ddef876e3674b641da7574 to your computer and use it in GitHub Desktop.
ThreadLocal example
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("press to start");
Console.ReadLine();
var threadLocal = new ThreadLocal<List<long>>(Creator, true);
Task.Run(async () =>
{
List<Task> tl = new List<Task>();
for (int i = 0; i < 10000; i++)
{
var task = Task.Run(() =>
{
var list = threadLocal.Value;
});
tl.Add(task);
}
await Task.WhenAll(tl);
Console.WriteLine(threadLocal.Values.Count);
});
Console.ReadLine();
}
static List<long> Creator() => new List<long>(1024 * 1024);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment