Channel Buffering in Ada
Based on the provided input, here’s the translation of the Go code to Ada, formatted in Markdown suitable for Hugo:
Ada does not have built-in channels like Go, so we’ll use a protected object to simulate a buffered channel. This example demonstrates how to create a buffered “channel” in Ada using a protected object with a fixed-size array.
In this Ada example, we’ve created a Buffered_Channel
protected type that simulates a buffered channel. The Buffer_Size
constant is set to 2, allowing the channel to buffer up to 2 values.
The Put
entry adds items to the buffer, while the Get
entry retrieves items from the buffer. The Count
variable keeps track of the number of items in the buffer, and In_Index
and Out_Index
manage the circular buffer.
In the main procedure, we demonstrate how to use this buffered channel:
- We create an instance of
Buffered_Channel
calledMessages
. - We put two strings into the channel using
Messages.Put
. - We then retrieve and print these two values using
Messages.Get
.
This example shows how Ada can implement a concept similar to buffered channels, providing a way to send multiple values without immediate corresponding receives.
To compile and run this Ada program:
This example demonstrates how Ada can implement buffered communication between tasks, which is conceptually similar to buffered channels in other languages.