An object created separately by different threads is still shared












1















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);
}
}









share|improve this question




















  • 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
















1















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);
}
}









share|improve this question




















  • 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














1












1








1








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);
}
}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












1 Answer
1






active

oldest

votes


















5














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?






share|improve this answer
























  • 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 use static all that often

    – Marc Gravell
    Nov 26 '18 at 12:43












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
});


}
});














draft saved

draft discarded


















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









5














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?






share|improve this answer
























  • 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 use static all that often

    – Marc Gravell
    Nov 26 '18 at 12:43
















5














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?






share|improve this answer
























  • 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 use static all that often

    – Marc Gravell
    Nov 26 '18 at 12:43














5












5








5







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?






share|improve this answer













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?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 12:39









Marc GravellMarc 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 use static 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






  • 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

















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Fotorealismo