An object created separately by different threads is still shared
I have a program in which I want to Simulate a queue. To speed things up (lots of different parameters) I thought that I could use a parallel loop, however the queue object (or at least the objects within that object) are still shared while they are all created within either the MGcC function or the queue object. Is there something I forgot about parallel functions?
The object which gives the trouble is the queue.MyHeap.
(Also if more information is needed please ask as I have left out a lot to make it more readably as you might see in the queue object).
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);
}
}
c# parallel.foreach
add a comment |
I have a program in which I want to Simulate a queue. To speed things up (lots of different parameters) I thought that I could use a parallel loop, however the queue object (or at least the objects within that object) are still shared while they are all created within either the MGcC function or the queue object. Is there something I forgot about parallel functions?
The object which gives the trouble is the queue.MyHeap.
(Also if more information is needed please ask as I have left out a lot to make it more readably as you might see in the queue object).
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);
}
}
c# parallel.foreach
1
Which part of theminHeap
is being shared? It doesn't use static fields by any chance?
– Me.Name
Nov 26 '18 at 12:35
what is the symptom that makes you think that something is being shared? what could we do to reproduce something being shared here, so we can understand what is happening?
– Marc Gravell♦
Nov 26 '18 at 12:35
The only thing I see shared iswriteFile
are you attempting to write to the same file from multiple threads?
– juharr
Nov 26 '18 at 12:37
MarcGravell The heap having multiple arrival objects. being empty. MeName It does, the list and counter (is that bad?).
– R.vW
Nov 26 '18 at 12:37
juharr, the file to write to is also different per thread
– R.vW
Nov 26 '18 at 12:40
add a comment |
I have a program in which I want to Simulate a queue. To speed things up (lots of different parameters) I thought that I could use a parallel loop, however the queue object (or at least the objects within that object) are still shared while they are all created within either the MGcC function or the queue object. Is there something I forgot about parallel functions?
The object which gives the trouble is the queue.MyHeap.
(Also if more information is needed please ask as I have left out a lot to make it more readably as you might see in the queue object).
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);
}
}
c# parallel.foreach
I have a program in which I want to Simulate a queue. To speed things up (lots of different parameters) I thought that I could use a parallel loop, however the queue object (or at least the objects within that object) are still shared while they are all created within either the MGcC function or the queue object. Is there something I forgot about parallel functions?
The object which gives the trouble is the queue.MyHeap.
(Also if more information is needed please ask as I have left out a lot to make it more readably as you might see in the queue object).
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);
}
}
c# parallel.foreach
c# parallel.foreach
edited Jan 16 at 13:33
Cœur
19.1k9114155
19.1k9114155
asked Nov 26 '18 at 12:30
R.vWR.vW
306
306
1
Which part of theminHeap
is being shared? It doesn't use static fields by any chance?
– Me.Name
Nov 26 '18 at 12:35
what is the symptom that makes you think that something is being shared? what could we do to reproduce something being shared here, so we can understand what is happening?
– Marc Gravell♦
Nov 26 '18 at 12:35
The only thing I see shared iswriteFile
are you attempting to write to the same file from multiple threads?
– juharr
Nov 26 '18 at 12:37
MarcGravell The heap having multiple arrival objects. being empty. MeName It does, the list and counter (is that bad?).
– R.vW
Nov 26 '18 at 12:37
juharr, the file to write to is also different per thread
– R.vW
Nov 26 '18 at 12:40
add a comment |
1
Which part of theminHeap
is being shared? It doesn't use static fields by any chance?
– Me.Name
Nov 26 '18 at 12:35
what is the symptom that makes you think that something is being shared? what could we do to reproduce something being shared here, so we can understand what is happening?
– Marc Gravell♦
Nov 26 '18 at 12:35
The only thing I see shared iswriteFile
are you attempting to write to the same file from multiple threads?
– juharr
Nov 26 '18 at 12:37
MarcGravell The heap having multiple arrival objects. being empty. MeName It does, the list and counter (is that bad?).
– R.vW
Nov 26 '18 at 12:37
juharr, the file to write to is also different per thread
– R.vW
Nov 26 '18 at 12:40
1
1
Which part of the
minHeap
is being shared? It doesn't use static fields by any chance?– Me.Name
Nov 26 '18 at 12:35
Which part of the
minHeap
is being shared? It doesn't use static fields by any chance?– Me.Name
Nov 26 '18 at 12:35
what is the symptom that makes you think that something is being shared? what could we do to reproduce something being shared here, so we can understand what is happening?
– Marc Gravell♦
Nov 26 '18 at 12:35
what is the symptom that makes you think that something is being shared? what could we do to reproduce something being shared here, so we can understand what is happening?
– Marc Gravell♦
Nov 26 '18 at 12:35
The only thing I see shared is
writeFile
are you attempting to write to the same file from multiple threads?– juharr
Nov 26 '18 at 12:37
The only thing I see shared is
writeFile
are you attempting to write to the same file from multiple threads?– juharr
Nov 26 '18 at 12:37
MarcGravell The heap having multiple arrival objects. being empty. MeName It does, the list and counter (is that bad?).
– R.vW
Nov 26 '18 at 12:37
MarcGravell The heap having multiple arrival objects. being empty. MeName It does, the list and counter (is that bad?).
– R.vW
Nov 26 '18 at 12:37
juharr, the file to write to is also different per thread
– R.vW
Nov 26 '18 at 12:40
juharr, the file to write to is also different per thread
– R.vW
Nov 26 '18 at 12:40
add a comment |
1 Answer
1
active
oldest
votes
All the fields on your minHeap
type are static
. So yes: they're shared - that's what static
means. You probably want to make them non-static
.
Possibly you used static
when you meant readonly
?
Yup that was the problem (still used to add static to must things as a beginner). Thanks
– R.vW
Nov 26 '18 at 12:42
2
@R.vW oh yeah, get out of that habit ASAP; it is very unlikely that you want to usestatic
all that often
– Marc Gravell♦
Nov 26 '18 at 12:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481171%2fan-object-created-separately-by-different-threads-is-still-shared%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
All the fields on your minHeap
type are static
. So yes: they're shared - that's what static
means. You probably want to make them non-static
.
Possibly you used static
when you meant readonly
?
Yup that was the problem (still used to add static to must things as a beginner). Thanks
– R.vW
Nov 26 '18 at 12:42
2
@R.vW oh yeah, get out of that habit ASAP; it is very unlikely that you want to usestatic
all that often
– Marc Gravell♦
Nov 26 '18 at 12:43
add a comment |
All the fields on your minHeap
type are static
. So yes: they're shared - that's what static
means. You probably want to make them non-static
.
Possibly you used static
when you meant readonly
?
Yup that was the problem (still used to add static to must things as a beginner). Thanks
– R.vW
Nov 26 '18 at 12:42
2
@R.vW oh yeah, get out of that habit ASAP; it is very unlikely that you want to usestatic
all that often
– Marc Gravell♦
Nov 26 '18 at 12:43
add a comment |
All the fields on your minHeap
type are static
. So yes: they're shared - that's what static
means. You probably want to make them non-static
.
Possibly you used static
when you meant readonly
?
All the fields on your minHeap
type are static
. So yes: they're shared - that's what static
means. You probably want to make them non-static
.
Possibly you used static
when you meant readonly
?
answered Nov 26 '18 at 12:39
Marc Gravell♦Marc Gravell
793k19821592563
793k19821592563
Yup that was the problem (still used to add static to must things as a beginner). Thanks
– R.vW
Nov 26 '18 at 12:42
2
@R.vW oh yeah, get out of that habit ASAP; it is very unlikely that you want to usestatic
all that often
– Marc Gravell♦
Nov 26 '18 at 12:43
add a comment |
Yup that was the problem (still used to add static to must things as a beginner). Thanks
– R.vW
Nov 26 '18 at 12:42
2
@R.vW oh yeah, get out of that habit ASAP; it is very unlikely that you want to usestatic
all that often
– Marc Gravell♦
Nov 26 '18 at 12:43
Yup that was the problem (still used to add static to must things as a beginner). Thanks
– R.vW
Nov 26 '18 at 12:42
Yup that was the problem (still used to add static to must things as a beginner). Thanks
– R.vW
Nov 26 '18 at 12:42
2
2
@R.vW oh yeah, get out of that habit ASAP; it is very unlikely that you want to use
static
all that often– Marc Gravell♦
Nov 26 '18 at 12:43
@R.vW oh yeah, get out of that habit ASAP; it is very unlikely that you want to use
static
all that often– Marc Gravell♦
Nov 26 '18 at 12:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481171%2fan-object-created-separately-by-different-threads-is-still-shared%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Which part of the
minHeap
is being shared? It doesn't use static fields by any chance?– Me.Name
Nov 26 '18 at 12:35
what is the symptom that makes you think that something is being shared? what could we do to reproduce something being shared here, so we can understand what is happening?
– Marc Gravell♦
Nov 26 '18 at 12:35
The only thing I see shared is
writeFile
are you attempting to write to the same file from multiple threads?– juharr
Nov 26 '18 at 12:37
MarcGravell The heap having multiple arrival objects. being empty. MeName It does, the list and counter (is that bad?).
– R.vW
Nov 26 '18 at 12:37
juharr, the file to write to is also different per thread
– R.vW
Nov 26 '18 at 12:40