I am trying to implement the KThread.join() method in nachos OS. My sample code fragment is as follows:
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();
}
My questions is If I create a parent thread and inside it I create a child and call child.join() method, then what will happen to the parent thread? According to me It will go to sleep until the child thread has finished it's task. Am I right? I have some more questions on this:
- when will the finish() method will be called? Is it called for both the parent and child thread?
- Inside the finish() method what is actually being done in the lines I have commented out?