I've read some things on threading and know that I should lock up a variable that is being accessed by multiple threads. The problem that I am experiencing is that the value in MainQueueToWorkingQueue(x)
is always three if I kick off the thread by first allocating the thread, then calling start in the second for loop.
But, if I do this new Thread(() => MainQueueToWorkingQueue(x)).Start();
then the code runs as expected and the correct number is passed.
private static List<BlockingQueue> WorkingQueueList = new List<BlockingQueue>();
static void Main(string[] args)
{
for(Int32 WorkingQueueListInsers = 0; WorkingQueueListInsert < 3; WorkinfQueueListInsert++)
{
WorkingQueueList.Add(new BlockingQueue(20));
}
Thread[] MainThreads = new Thread[WorkingQueueList.Count];
for (Int32 x = 0; x < WorkingQueueList.Count; x++)
{
/* MainThreads[x] = */ new Thread(() => MainQueueToWorkingQueue(x)).Start();
Thread.Sleep(50);
}
for (Int32 x = 0; x < WorkingQueueList.Count; x++)
{
MainThreads[x].Start();
Thread.Sleep(50);
}
Console.Read();
}
private static void MainQueueToWorkingQueue(Int32 WorkingQueuePosition)
{
/* if new Thread(() => MainQueueToWorkingQueue(x)).Start(); is called
then WorkingQueuePosition is correct and is either zero, one, or two. */
/* if the thread is allocated in one for loop, then started in another for loop, WorkingQueuePosition only equals three */
Console.WriteLine("Ending x: " + WorkingQueuePosition);
while (true)
{
WorkingQueueList[WorkingQueuePosition].Enqueue(MainQueue.Dequeue());
}
}
My question is this. Why is the passed parameter correct when I use Start()
when making the new thread, but the passed parameter is always three when I call Start()
in a second for loop?
My guess: I know somewhere the parameter is being altered. My first guess was that the loop was running to fast that the thread used a different value than the one passed because x was updated too quickly. I tried fixing that with a Thread.Sleep(50)
but the issue still remained.
EDIT: Took out code that did not deal directly with the problem.