NACHOS C++: Thread Fork to create data structures
I have a program that runs perfectly well when I have declare and initialize my List data structure at the top and then call my function generateID . It also works if I declare the List at the top and initialize the List inside the function. However the problem I am having is using threads to create the List. I keep getting segmentation errors.
At the top of my program, I have my declarations.
List* aLine;
At the bottom, I have two functions.
void CreateListA(int which)
{
aLine = new List;
currentThread->Yield();
}
void ThreadTest()
{
Thread *gA = new Thread("Creates new Li开发者_JAVA技巧st A");
gA->Fork(CreateListA, 1);
generateID();
}
Now, when I run thread test, I get segmentation errors. I am guessing that some where when creating the Lists with threads, memory got all messed up. But I can't figure out why there will be a problem with this? I created a player object the same way (with threads) and the program worked fine. Now I am trying to create the List data structure and it fails. ***Note generateID() uses append and remove to manipulate the list.
After you Fork the new thread, generateID() is executed immediately: the thread may not have started yet or it could be in the middle of the creation of the list.
Maybe generateID() should be the function in the different thread and the creation of the list should be in the main one.
I suppose that Fork
create a new thread, so, is possible that genereteID()
, in the main thread, manipulate the list without been created yet. Try to invoke genereteID()
in the thread, been sure of the list was really created. If not, check in genereteID()
the correct creation and list initialization.
精彩评论