c# - Multithreading doesn't work if I start more than 1 other thread -
i don't know if problem unity-specific maybe other c# developers can me :)
so trying implement own version of a* algorithm , improve performance want actual path finding every seeker multi threaded:
it works pretty until moment activate second seeker:
like can see stops after 15 iterations (varying depending on how many text outputs call -> guess threads getting killed after amount of time) in algorithm , first started thread gets kind of killed following.
i using c# threadpool
class thread management tried use thread
class. same result.
implementation of threading:
public static void requestpath(pathrequest _request) { debug.log("queueing pathrequest..."); threadpool.queueuserworkitem(instance.startthreadedpathfinding, _request); }
the called method:
private void startthreadedpathfinding(object _stateinfo) { pathrequest request = (pathrequest)_stateinfo; m_pathfinder.findpath(request, onpathfound); }
and findpath
:
public void findpath(pathrequest _request, action<pathresult> _callback) { debug.log("starting a* algorithm"); binaryheap<node> openlist = new binaryheap<node>(gridsize); hashset<node> closedlist = new hashset<node>(); node startnode = _request.start; node targetnode = _request.target; bool success = false; openlist.add(startnode); while (openlist.count > 0) { debug.log(thread.currentthread.managedthreadid + ": " + thread.currentthread.threadstate.tostring()); node currentnode = openlist.removefirst(); if (currentnode == targetnode) { // todo: path found -> _callback success = true; debug.log("path found"); _callback(new pathresult(null, success, _request.callback)); return; } closedlist.add(currentnode); foreach (node neighbour in currentnode.m_neighbours) { if (closedlist.contains(neighbour)) { continue; } int tentativeg = currentnode.m_gcost + getdistance(currentnode, neighbour); if (openlist.contains(neighbour) && tentativeg > neighbour.m_gcost) { continue; } neighbour.m_parent = currentnode; neighbour.m_gcost = tentativeg; neighbour.m_hcost = getdistance(neighbour, targetnode); if (openlist.contains(neighbour)) { openlist.updateitem(neighbour); } else { openlist.add(neighbour); } } } // todo: no path target exists -> calculate path success = false; debug.log("no existing path"); _callback(new pathresult(null, success, _request.callback)); return; }
requestpath
, startthreadedpathfinding
in class called pathrequestmanager
, findpath
in other class called pathfinder
.
another point threads don't stop because of error or sth still somehow running think because after started scene in unity have kill unity-process in taskmanager because sth stuck (cpu-load 80% when have that) thought deadlocks couldn't find any.
i glad if me here , if need more information source code feel free ask :)
so found problem answer in unity forums:
i working on same grid
, therefore on same nodes
every thread. when updating node
in algorithm on 1 thread while 1 working same node
bad things happend , result can seen above in question.
i solved cloning grid each thread , working on grids owned threads.
Comments
Post a Comment