Channel Directions in Ada
Ada doesn’t have built-in channels like Go, so we’ll use Ada’s tasking system to simulate similar behavior. We’ll use protected objects to ensure thread-safe communication between tasks.
This Ada program demonstrates a concept similar to channel directions using Ada’s tasking system:
We define a protected type
Message_Queue
that acts like a channel, withSend
andReceive
entries.The
Ping
task sends a message to thePings
queue.The
Pong
task receives a message fromPings
and sends it toPongs
.In the main procedure, we wait a short time for the tasks to complete, then receive and print the message from
Pongs
.
To run this program, save it as channel_directions.adb
and compile it with an Ada compiler:
This example demonstrates how Ada’s tasking system can be used to achieve similar functionality to Go’s channel directions. While the syntax and concepts are different, the core idea of safe, directional communication between concurrent processes is preserved.