❌ About FreshRSS

Normal view

There are new articles available, click to refresh the page.
Before yesterdayAda Forum - Latest topics

Question about vector initialization

By: cunger
18 December 2023 at 06:38

Yesterday, I was trying to do something with an array of vectors and stumbled a bit. So I’d like to ask some clarification questions about initialization…

With arrays (as with most other types), a variable is uninitialized until it’s explicitly set to a value.

My_List : array (1 .. 10) of Boolean;   -- uninitialized
My_List := [1 => True, other => False];

With vectors it’s slightly different. The reference manual says:

If an object of type Vector is not otherwise initialized, it is initialized to the same value as Empty_Vector.

So if I have:

package Boolean_Vectors is Ada.Containers.Vectors (
  Index_Type   => Positive,
  Element_Type => Boolean
);

subtype Boolean_Vector is Boolean_Vectors.Vector;

I can actually do:

My_Vector : Boolean_Vector; -- Implicitly initialized here?
My_Vector.Append (True);    -- Or here?
My_Vector.Append (False);

Although I’m not sure when exactly initialization happens (and why it was designed to happen implicitly).

Now, Empty_Vector is a constant, so I cannot use it if I want to initialize an empty vector explicitly.
So, what do I do when I want to have an array of vectors?

My_Vector_List : array (1 .. 10) of Boolean_Vector;
My_Vector_List := [others => ??]; -- How do I put an empty vector here
                                  -- that can grow later?

Or do I have to do it like this?

My_Vector_1, My_Vector_2 : Boolean_Vector;

My_Vector_List (1) := My_Vector_1;
My_Vector_List (2) := My_Vector_2;
...

9 posts - 7 participants

Read full topic

Understanding `select or delay until` in tasks

By: cunger
17 November 2023 at 06:29

I’m playing with tasks and have a question about select with delay until.

I have a task with the following body (the full implementation is on GitHub):

Wait : loop
   select
      -- Wait for pings. When they come, reset Last_Ping.
      accept Ping do
          Log.Debug ("Received ping.");
          Last_Ping := Clock;
      end Ping;
   or
      -- If there is no ping within the expected interval, do something.
      delay until (Last_Ping + Expected_Ping_Interval);
      Log.Error ("Timeout!");
      -- Do something.
      -- Is there any way to keep running the task loop here? 
   end select;
end loop Wait;

From how I understand the select, it waits for the specified delay - if there is a Ping before the end of the delay, the loop iteration is finished and the next iteration is started, again waiting for the specified delay or accepting a Ping.

Now, when the delay is over and there was no entry in Ping, it does not proceed with the next loop iteration but is finished, right? Why is that? And is there a way to write this loop so the task continues running?

(Also happily take pointers to good resources for learning about task behavior in more depth. :slight_smile:)

6 posts - 3 participants

Read full topic

❌
❌