一个由不同线程分别创建的对象仍然是共享的
An object created separately by different threads is still shared
我有一个程序,我想在其中模拟一个队列。为了加快速度(很多不同的参数)我认为我可以使用并行循环,但是队列对象(或至少该对象中的对象)仍然共享,而它们都是在 MGcC 函数或队列中创建的目的。有什么我忘记了并行功能吗?
出事的对象是queue.MyHeap。
(另外,如果需要更多信息,请询问,因为我已经遗漏了很多内容以使其更具可读性,正如您在队列对象中看到的那样)。
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);
}
}
您 minHeap
类型的所有字段都是 static
。所以是的:它们是共享的——这就是 static
的意思。您可能想让它们成为非 static
.
可能你用的是static
,而你的意思是readonly
?
我有一个程序,我想在其中模拟一个队列。为了加快速度(很多不同的参数)我认为我可以使用并行循环,但是队列对象(或至少该对象中的对象)仍然共享,而它们都是在 MGcC 函数或队列中创建的目的。有什么我忘记了并行功能吗?
出事的对象是queue.MyHeap。
(另外,如果需要更多信息,请询问,因为我已经遗漏了很多内容以使其更具可读性,正如您在队列对象中看到的那样)。
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);
}
}
您 minHeap
类型的所有字段都是 static
。所以是的:它们是共享的——这就是 static
的意思。您可能想让它们成为非 static
.
可能你用的是static
,而你的意思是readonly
?