我正在尝试在 nachos OS 中实现 KThread.join() 方法。我的示例代码片段如下:
private KThread toJoin = null;
public void join() {
Lib.debug(dbgThread, "Joining to thread: " + toString());
Lib.assertTrue(this != currentThread);
Lib.assertTrue(toJoin == null);
boolean intStatus = Machine.interrupt().disable();
if (status != statusFinished)
{
toJoin = KThread.currentThread();
KThread.sleep();
}
Machine.interrupt().restore(intStatus);
}
public static void finish() {
Lib.debug(dbgThread, "Finishing thread: " + currentThread.toString());
Machine.interrupt().disable();
Machine.autoGrader().finishingCurrentThread();
Lib.assertTrue(toBeDestroyed == null); // what is being done in this line?
toBeDestroyed = currentThread;
if (currentThread.toJoin != null)
{
currentThread.toJoin.ready(); ////what is being done in this line?
}
currentThread.status = statusFinished;
sleep();
}
我的问题是如果我创建一个父线程并在其中创建一个子线程并调用 child.join() 方法,那么父线程会发生什么?据我说,它会进入睡眠状态,直到子线程完成它的任务。我对吗?我对此还有一些问题:
- 什么时候会调用 finish() 方法?是否同时为父线程和子线程调用?
- 在finish() 方法中,我注释掉的行中实际做了什么?