Unix 线程本地聚合和任务并行库
为什么我会在代码的以下部分得到不同的结果 代码示例1Unix 线程本地聚合和任务并行库,unix,concurrency,parallel-processing,task-parallel-library,thread-local,Unix,Concurrency,Parallel Processing,Task Parallel Library,Thread Local,为什么我会在代码的以下部分得到不同的结果 代码示例1 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ThreadLocalTasks { class Program { static void Main(stri
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ThreadLocalTasks
{
class Program
{
static void Main(string[] args)
{
ThreadLocal<int> aggregrations = new ThreadLocal<int>();
Task<int>[] tasks = new Task<int>[10];
for (int i = 0; i < tasks.Length; i++)
{
aggregrations.Value = 0;
int tempi = i;
tasks[tempi] = new Task<int>(() =>
{
int temp = 0;
for (int j = 1; j <= 3; j++)
{
temp += j;
}
aggregrations.Value = temp;
return aggregrations.Value;
});
}
tasks.ToList().ForEach(x => {
x.Start();
});
Task.WaitAll(tasks);
int sum = 0;
tasks.ToList().ForEach(x =>
{
sum += x.Result;
});
Console.WriteLine("Sum: {0}", sum);
Console.WriteLine("Press any key to quit..");
Console.ReadKey();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统线程;
命名空间线程本地任务
{
班级计划
{
静态void Main(字符串[]参数)
{
ThreadLocal aggregations=新的ThreadLocal();
任务[]任务=新任务[10];
for(int i=0;i
{
内部温度=0;
对于(int j=1;j{
x、 Start();
});
Task.WaitAll(任务);
整数和=0;
tasks.ToList().ForEach(x=>
{
总和+=x.结果;
});
WriteLine(“Sum:{0}”,Sum);
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
}
样本2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ThreadLocalTasks
{
class Program
{
static void Main(string[] args)
{
ThreadLocal<int> aggregrations = new ThreadLocal<int>();
Task<int>[] tasks = new Task<int>[10];
for (int i = 0; i < tasks.Length; i++)
{
aggregrations.Value = 0;
int tempi = i;
tasks[tempi] = new Task<int>(() =>
{
for (int j = 1; j <= 3; j++)
{
aggregrations.Value += j;
}
return aggregrations.Value;
});
}
tasks.ToList().ForEach(x => {
x.Start();
});
Task.WaitAll(tasks);
int sum = 0;
tasks.ToList().ForEach(x =>
{
sum += x.Result;
});
Console.WriteLine("Sum: {0}", sum);
Console.WriteLine("Press any key to quit..");
Console.ReadKey();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用系统线程;
命名空间线程本地任务
{
班级计划
{
静态void Main(字符串[]参数)
{
ThreadLocal aggregations=新的ThreadLocal();
任务[]任务=新任务[10];
for(int i=0;i
{
对于(int j=1;j{
x、 Start();
});
Task.WaitAll(任务);
整数和=0;
tasks.ToList().ForEach(x=>
{
总和+=x.结果;
});
WriteLine(“Sum:{0}”,Sum);
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
}
为什么您要在这里使用ThreadLocal
存储,而不仅仅是任务中的局部变量?任务并行库很可能会重用一个线程来执行多个任务,而您的线程本地存储将被覆盖。在第一个示例中,它可能会起作用,因为您没有在每次运行线程时重置它重复使用,但这会更好:
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = new Task<int>(() =>
{
int sum = 0;
for (int j = 1; j <= 3; j++)
{
sum += j;
}
return sum;
});
}
for(int i=0;i
{
整数和=0;
对于(int j=1;j为什么您要在这里使用ThreadLocal
存储,而不仅仅是任务中的局部变量?任务并行库很可能会重用一个线程来执行多个任务,并且您的线程本地存储将被覆盖。在第一个示例中,它可能会工作,因为您不会每次都重置它可以重复使用read,但这样会更好:
for (int i = 0; i < tasks.Length; i++)
{
tasks[i] = new Task<int>(() =>
{
int sum = 0;
for (int j = 1; j <= 3; j++)
{
sum += j;
}
return sum;
});
}
for(int i=0;i
{
整数和=0;
对于(int j=1;j),与其说是关于为什么要这样做,不如说更多的是关于这个技术正确性。我添加了一个更详细的解释,解释了为什么两个示例都“不正确”。与其说是关于为什么要这样做,不如说是关于这个技术正确性。我添加了一个更详细的解释,解释了为什么两个示例都“不正确”。