Why it is not sure for below code that task_1 should execute first then task_2 because task_1 which is in waiting state get first start message from main? but output is not predictable please help me to understand?
WITH Ada.Text_IO; -- Include Text_IO Library
WITH Ada.Integer_Text_IO; -- Include Integer Text_IO Library
PROCEDURE task_demo_02 IS
TASK TYPE intro_task (message1 : Integer) IS
ENTRY start; -- Entry Point Into The Task
END intro_task;
TASK BODY intro_task IS -- Task Body Definition
BEGIN
ada.text_io.put_line("waitng");
ACCEPT start; -- Entry Point Into The Task
Ada.Text_IO.put ( "Display from Task ");
Ada.Integer_Text_IO.put (message1, 1);
Ada.Text_IO.new_line;
END intro_task;
Task_1 : intro_task (message1 => 1);--activate
Task_2 : intro_task (message1 => 2);--activate
BEGIN
ada.Text_IO.put_line("main");
Task_1.start; --- first task_1 should execute
Task_2.start; --- then task_2 isn't it ?
END task_demo_02;
BEGIN
ada.Text_IO.put_line("main");
Task_1.start; --- first task_1 should execute
Your comment seems to assume that this will cause Task_1 to execute, and Task_1 will finish executing until it's done, and then the main program will go on to Task_2.start.
But that's not the case. Task_1.start means that Task_1 is now ready to execute. But at this point, there are two tasks ready to execute: Task_1, and the environment task, which is the task that's running task_demo_2. You can't really tell which task is run first (there are some dispatching policies that will specify how tasks get run in a case like this, but in general you can't tell). That means that task_demo_02 could keep running, which means Task_2.start will happen before Task_1continues. Or it could mean that Task_1 starts, but that the processor will split time between Task_1 and task_demo_02. Or, on a multi-processor system, they could both run in parallel.
If you want to write a program where the main program starts Task_1 and waits for it to finish, the most reliable way is to add another entry:
TASK TYPE intro_task (message1 : Integer) IS
entry Start;
entry Finish;
END intro_task;
Task_1.Start;
Task_1.Finish;
and then, at the end of intro_task, add
accept Finish;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With