父母子线程没有正常退出

时间:2015-07-27 11:22:14

标签: c# multithreading unity3d

需要一些关于多线程的帮助。我在统一的单独线程中进行一些计算,并在完成所有线程时呈现结果。

以下是我的代码结构。

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

每次使用Join()时,Thread 1.1及其子节点(Thread 1.1.2.1 ..)都会正常退出,但Thread 1.2不是这种情况;线程1.2.1.1 ..退出而不完成代码块。对于线程1.2,看起来像childThreadList上的Join()没有阻塞当前线程。

代码很简单,可以生成大型列表块并并行处理它们。我之前遇到的问题是Thread 1.1和Thread 1.2在中途退出;原因是他们在.Start()时共享相同的变量值。我确保现在不是这个原因。

我有什么错误或遗失,请提供建议。

示例代码:

//Running in Thread 1; started from main()
private unsafe void AddSurfaceData()
{
    float* points;
    int points_length;

    float* colours;
    int colours_length;

    System.UIntPtr *indices;
    int indices_length;

    //set references for 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 from = initialLength;
            int to = currLength;

            //Add surfaceData
            int surfaceIndex = surfaceData.Count-1;

            //Start thread from initialLength to 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 from = initialLength;
            int to = 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 from initialLength to 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 from = initialLength;
            int to = currLength;

            //Start thread from initialLength to 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 from = initialLength;
            int to = indicesLength;

            //Start thread from initialLength to 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)
    }
}

0 个答案:

没有答案
相关问题