C# 由不同线程分别创建的对象仍然是共享的
我有一个程序,我想在其中模拟一个队列。为了加快速度(许多不同的参数),我认为我可以使用并行循环,但是队列对象(或至少该对象中的对象)仍然是共享的,而它们都是在MGcC函数或队列对象中创建的。并行函数有什么我忘记的吗 产生问题的对象是queue.MyHeap (另外,如果需要更多信息,请询问,因为我遗漏了很多信息,以使其更易于阅读,正如您可能在队列对象中看到的那样)C# 由不同线程分别创建的对象仍然是共享的,c#,parallel.foreach,C#,Parallel.foreach,我有一个程序,我想在其中模拟一个队列。为了加快速度(许多不同的参数),我认为我可以使用并行循环,但是队列对象(或至少该对象中的对象)仍然是共享的,而它们都是在MGcC函数或队列对象中创建的。并行函数有什么我忘记的吗 产生问题的对象是queue.MyHeap (另外,如果需要更多信息,请询问,因为我遗漏了很多信息,以使其更易于阅读,正如您可能在队列对象中看到的那样) Parallel.ForEach(a,(数字)=> { MGcC(a); });
Parallel.ForEach(a,(数字)=>
{
MGcC(a);
});
静态公共元组MGcC(int a)
{
队列=新队列(a);
返回queue.Simulate(writeFile);
}
公共类队列
{
函数到达函数;
函数服务函数;
双λ;
双v;
对象服务对象;
int minserver;
布尔判决;
int idleserver;
int-activeserver;
int-amountInOrbit;
受保护的minHeap-myHeap;
公共队列(双lambda、双v、对象serviceObject、int服务器、Func到达函数、Func服务函数、bool decision=false)
{
this.arrivalFunction=arrivalFunction;
this.servingFunction=服务功能;
this.lambda=lambda;
这个,v=v;
this.serviceObject=serviceObject;
this.minServers=服务器;
这个决定=决定;
idleserver=服务器;
activeServers=0;
amountInOrbit=0;
myHeap=新的minHeap();
}
公共类minHeap
{
静态保护动作[]堆;
静态公共整数计数器;
公共垃圾堆()
{
计数器=-1;
堆=新操作[1000000];
}
公共行动
{
如果(计数器<0)
{
控制台。写入线(“空”);
返回新操作(0,0,新客户());
}
Action returnValue=heap[0];
堆[0]=堆[计数器];
计数器--;
heapify(0);
返回值(returnValue);
}
公共无效推送(行动a)
{
计数器++;
heap[counter]=新操作(double.PositiveInfinity,0,new Customer());
插入键(计数器,a);
}
静态void InsertKey(inti,动作a)
{
if(heap[i].TimeOfExecution0&&heap[Parent(i)]。执行时间>堆[i]。执行时间)
{
动作温度=堆[i];
堆[i]=堆[Parent(i)];
堆[父(i)]=温度;
i=父母(i);
}
}
您的minHeap
类型上的所有字段都是静态的
。所以是的:它们是共享的-这就是静态的
的意思。您可能希望将它们设为非静态的
当您指的是
只读时,您可能使用了静态
?您的minHeap
类型上的所有字段都是静态
。因此,是的:它们是共享的-这就是静态
的意思。您可能希望将它们设置为非静态
当您指的是只读时,您可能使用了static
?正在共享minHeap
的哪个部分?它不可能使用静态字段?什么症状使您认为某些内容正在被共享?我们可以做些什么来复制共享的内容,以便我们能够了解发生了什么ng?我看到的唯一共享内容是writeFile
您是否试图从多个线程写入同一个文件?marcGravel堆有多个到达对象。如果为空,则列表和计数器(坏吗?).juharr,每个线程要写入的文件也不同,minHeap
的哪个部分正在被共享?它不可能使用静态字段?是什么症状让你认为某个东西正在被共享?我们可以做些什么来复制共享的东西,这样我们就可以了解发生了什么?我看到的唯一一件事是shared iswriteFile
您是否试图从多个线程写入同一文件?MarcGravel堆中有多个到达对象。为空。MeName它是空的,列表和计数器(是否坏?)。juharr,每个线程要写入的文件也不同这就是问题所在(作为初学者,仍然用于添加静态内容)。Thanks@R.vW哦,是的,尽快改掉这个习惯;你不太可能经常使用static
这就是问题所在(作为初学者,仍然习惯于将static添加到必须的东西中). Thanks@R.vW哦,是的,尽快改掉这个习惯;你不太可能经常使用static
Parallel.ForEach(a, (numbers) =>
{
MGcC(a);
});
static public Tuple<Customer[,], List<Interval>[]> MGcC(int a)
{
Queue queue = new Queue(a);
return queue.Simulate(writeFile);
}
public class Queue
{
Func<object, double> arrivalFunction;
Func<object, double> servingFunction;
double lambda;
double v;
object serviceObject;
int minServers;
bool decision;
int idleServers;
int activeServers;
int amountInOrbit;
protected minHeap myHeap;
public Queue(double lambda, double v, object serviceObject, int servers, Func<object, double> arrivalFunction, Func<object, double> servingFunction, bool decision = false)
{
this.arrivalFunction = arrivalFunction;
this.servingFunction = servingFunction;
this.lambda = lambda;
this.v = v;
this.serviceObject = serviceObject;
this.minServers = servers;
this.decision = decision;
idleServers = servers;
activeServers = 0;
amountInOrbit = 0;
myHeap = new minHeap();
}
public class minHeap
{
static protected Action[] heap;
static public int counter;
public minHeap()
{
counter = -1;
heap = new Action[1000000];
}
public Action Pop()
{
if (counter < 0)
{
Console.WriteLine("empty");
return new Action(0, 0, new Customer());
}
Action returnValue = heap[0];
heap[0] = heap[counter];
counter--;
heapify(0);
return (returnValue);
}
public void Push(Action a)
{
counter++;
heap[counter] = new Action(double.PositiveInfinity, 0, new Customer());
InsertKey(counter, a);
}
static void InsertKey(int i, Action a)
{
if (heap[i].TimeOfExecution < a.TimeOfExecution)
Console.WriteLine("should not have happened");
heap[i] = a;
while (i > 0 && heap[Parent(i)].TimeOfExecution > heap[i].TimeOfExecution)
{
Action temp = heap[i];
heap[i] = heap[Parent(i)];
heap[Parent(i)] = temp;
i = Parent(i);
}
}