MultiThreading Logic Error?

I have a function that isn’t making sense with mutithreading, because it is writing results from a different gameobject than called by the function:

function multicore_normals (go: GameObject ){

	yield WaitForSeconds(0.2);	
	//yield WaitForFixedUpdate;	
    var mesh : Mesh = go.GetComponent(MeshFilter).mesh;
    var indicies: int[] = mesh.triangles;
    var myvertices : Vector3[] = mesh.vertices;
    var mynormals: Vector3[] = mesh.normals;
	mynormals  = new Vector3[mesh.vertices.length];

Loom.RunAsync(function() {//----=-=-=-=-multicore from unitygems
	for(var i :int= 0; i < indicies.Length; i += 3)
		{
			var v0: Vector3= myvertices[indicies*];*
  •  	var v1 :Vector3= myvertices[indicies[i+1]];*
    
  •  	var v2 :Vector3= myvertices[indicies[i+2]];*
    
  •  	var  normal : Vector3= Vector3.Normalize(Vector3.Cross(v1 - v0, v2 - v0));*
    

_ mynormals[indicies*] += normal;_
_
mynormals[indicies[i+1]] += normal;_
_
mynormals[indicies[i+2]] += normal;*_

* }*
for(i = 0; i < myvertices.Length; i++)
{
mynormals = Vector3.Normalize(mynormals*);*
}
Loom.QueueOnMainThread(function() {//----=-=-=-=-

*//mesh.normals = mynormals ; *
});});//----=-=-=-=-

* yield WaitForFixedUpdate;*

* mesh.normals = mynormals ; *

}
Result, every 1000 or so objects, i find a GameObject that has normals applied to it, that belong to a different GameObject, or that haven’t been calculated at all for that object. how can it rewrite the vertices of the object, and then fail to write normals on it, or write normals from a different GameObject of unity?
To call the newnormals function, the code does:
get meshObject, rewrite vertices,
newnormals(meshObject)

You have some strange pseude code there... it's not clear on which data which thread is working and when you reassign the arrays to the mesh. How do you synchronise your worker thread with the main thread? A little bit actual code would help to understand what you're actually doing since your pseudo code doesn't make any sense...

1 Answer

1

You might want to take a look at the Loom class when it comes to threading.

hey bunny, it's using the loom class from unitygems. i've put the function that makes errors in the original post. the other day it ran for about 2k instances with no errors, i restarted my pc again and saw 5 errors in 400 instances of mesh.