NACHOS C++:线程分叉创建数据结构
当我在顶部声明并初始化 List 数据结构,然后调用我的函数generateID 时,我有一个运行得非常好的程序。如果我在顶部声明列表并在函数内初始化列表,它也可以工作。然而,我遇到的问题是使用线程来创建列表。我不断收到分段错误。
在我的程序的顶部,我有我的声明。
列表* aLine;
在底部,我有两个功能。
void CreateListA(int which)
{
aLine = new List;
currentThread->Yield();
}
void ThreadTest()
{
Thread *gA = new Thread("Creates new List A");
gA->Fork(CreateListA, 1);
generateID();
}
现在,当我运行线程测试时,出现分段错误。我猜测在使用线程创建列表时,内存会变得一团糟。但我不明白为什么这样会出现问题?我以相同的方式(使用线程)创建了一个播放器对象,并且程序运行良好。现在我尝试创建列表数据结构但失败了。 ***注意generateID()使用append和remove来操作列表。
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 List 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Fork 新线程后,generateID() 会立即执行:该线程可能尚未启动,或者可能正在创建列表的过程中。
也许generateID()应该是不同线程中的函数,并且列表的创建应该在主线程中。
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.
我认为 Fork 创建了一个新线程,因此,在主线程中,genereteID() 可能会在尚未创建的情况下操作列表。尝试在线程中调用genereteID(),确保列表确实被创建。如果没有,请检查
genereteID()
是否正确创建和列表初始化。I suppose that
Fork
create a new thread, so, is possible thatgenereteID()
, in the main thread, manipulate the list without been created yet. Try to invokegenereteID()
in the thread, been sure of the list was really created. If not, check ingenereteID()
the correct creation and list initialization.