c# - Parents child threads not exiting properly -
need little regarding multithreading. doing computation in separate threads in unity , rendering result when threads completed.
below structure of code.
thread 1 | | thread 1.1 thread 1.2 | | thread 1.1.2.1 thread 1.2.1.1 thread 1.1.2.2 thread 1.2.2.2 ... ...
every time thread 1.1 , child (thread 1.1.2.1..) exits using join() not case thread 1.2; thread 1.2.1.1.. exits without completing code block. looks join() on childthreadlist not blocking current thread in case of thread 1.2.
code simple making blocks of large list , process them in parallel. having same issue thread 1.1 , thread 1.2 exits in midway; reason sharing same variable values when on .start(). make sure not reason now.
is there mistaking or missing, please advice.
example code:
//running in thread 1; started main() private unsafe void addsurfacedata() { float* points; int points_length; float* colours; int colours_length; system.uintptr *indices; int indices_length; //set references points, colours, indices, points_length, colours_length, indices_length processtriangles(points, indices, colours, indices_length); } private unsafe void processtriangles(float* points, system.uintptr *indices, float* colours, int indices_length) { list<thread> threadlist = new list<thread>(); int currlength = 65535; int initiallength = 0; while(true) { if(currlength < indices_length) { int = initiallength; int = currlength; //add surfacedata int surfaceindex = surfacedata.count-1; //start thread initiallength currlength thread thread = new thread(() => addtriangle(from, to, indices, points, colours, surfaceindex)); threadlist.add(thread); thread1count++; thread.name = "thread 1."+thread1count; thread.start(); initiallength = currlength; currlength += 65535; } else { int = initiallength; int = indices_length; //add surfacedata surfacedata.add(new scanmanager.surfacedata(new vector3[to-from], new color[to-from], new int[to-from])); int surfaceindex = surfacedata.count-1; //start thread initiallength indices_length thread thread = new thread(() => addtriangle(from, to, indices, points, colours, surfaceindex)); threadlist.add(thread); thread1count++; thread.name = "thread 1."+thread1count; thread.start(); break; } } foreach(thread thread in threadlist) { thread.join(); } } private unsafe void addtriangle(int fromlength, int tolength, system.uintptr *indices, float* points, float* colours, int surfaceindex) { list<thread> childthreadlist = new list<thread>(); int initiallength = fromlength; int currlength = 21000+initiallength; int index = surfaceindex; int indiceslength = tolength; while(true) { if(currlength < indiceslength) { int = initiallength; int = currlength; //start thread initiallength currlength thread childthread = new thread(() => addtriangledata(from, to, indices, points, colours, index)); childthreadlist.add(childthread); thread2count++; childthread.name = thread.currentthread.name+".2."+thread2count; childthread.start(); initiallength = currlength; currlength += 21000; } else { int = initiallength; int = indiceslength; //start thread initiallength indices_length thread childthread = new thread(() => addtriangledata(from, to, indices, points, colours, index)); childthreadlist.add(childthread); thread2count++; childthread.name = thread.currentthread.name+".2."+thread2count; childthread.start(); break; } } foreach(thread childthread in childthreadlist) { childthread.join(); } } private unsafe void addtriangledata(int fromlength, int tolength, system.uintptr *indices, float* points, float* colours, int surfaceindex) { for(int i=fromlength; i<tolength; i+=3) { //addtrianglevertex (i) //addtrianglevertex (i+1) //addtrianglevertex (i+2) } }
Comments
Post a Comment