Itโs hard for me to understand why multithreading does not update the values โโuntil the stream ends. Does a separate thread have its own copy of links or values?
If not, as far as I know, the code below should work correctly when calling MyMethod, but often it does not instantiate some MyType objects in the array before thread.IsAlive becomes false:
class MyClass { static MyType[] obj = new MyType[Environment.ProcessorCount - 1]; void MyMethod() { Thread[] threads = new Thread[Environment.ProcessorCount - 1]; for (int i = 0; i < Environment.ProcessorCount - 1; i++) { threads[i] = new Thread(() => FillObjects(i)); threads[i].Priority = ThreadPriority.AboveNormal; threads[i].Start(); } while (threads[i].Any(c => c.IsAlive)) { Thread.Sleep(50); } } void FillObjects(int i) { obj[i] = new MyType();
Almis source share