i think i understand what is going on… this is a very different way of thinking for me. I think my head exploded. I was able to cast the WorkItem from the beginning but the pointers didn’t match up.
I could be way off base.
I allocated my worker on ‘the stack’
void CreateDungeon()
{
WorkItem item;
item.sendEvent_ = true;
item.workFunction_ = GenerateDungeonStart;
dungeonWorker = &item;
workQueue->AddWorkItem(item);
}
AddWorkItem doesn’t take a pointer to a work item so it ‘copies’ the variable. After the scope of the function the dungeonWorker actually gets ‘nullified’. I have no idea what it is pointing to after the item gets destroyed. It amuses me that I will utilize allocating variables on stack to make it so i don’t cleanup but now I didn’t think of this inverted scenario. My main motivation for using the pointer was to make it so i could track a work item and when the work item was completed to use the completed handler to and do some work based on the task that was completed.
The api seems to be suited for splitting up work to utiltize multiple cores more effectively. I think everytime i saw threads being used they ended up all blocking using the queue#complete function within the same frame. I can see that there is some code to try to prevent too many tasks from running long:
This is in the WorkerThread
It looks like GetSubsystem is a singleton? Since complete gets called every frame tasks would never be able to run more then a frame? If I wanted to have a task run longer would I want to make a new WorkQueue for my game specific code?
In the dreadful posibility of a single threaded system we might be able to add a WorkItem.current pointer to hold the current position of an iterator and suspend a task to pickup where it left off next frame for a longer running task as though as a sort of hackish coroutine. maybe we can make the work function return a bool for whether it has been completed or not?
also a posible improvement to the api would be for the ability to specifify a workitem in the subscribe method: