Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threads and semaphores in Ada95

How can I use threads in Ada95? What functions can I use to create, destroy, stop and start them?

How can I use semaphores in this language?

like image 697
szaman Avatar asked Dec 02 '22 06:12

szaman


2 Answers

Concurrency is built into the language, so you have specific Ada syntax for tasks (i.e. threads) and protected objects (i.e. which are more powerful than semaphores / mutexes / conditional variables). This makes much easier (and less error prone) programming multi-threaded apps in Ada than in other languages like C / Java.

It's not recommended to use semaphores in Ada, protected objects are much more powerful (but you can build semaphores easily using protected objects if needed).

Some small syntax examples. Tasks (and protected objects) can be static...

task My_Task;

task body My_Task is
begin
   -- Just print this to stdout and exit thread
   Ada.Text_IO.Put_Line("Hello, concurrent World!");
end;

...or created dynamically

task type My_Task_Type(N : Natural);

task body My_Task_Type(N : Natural) is ...

...

T1 := new My_Task_Type(100);

abort T1;

Much less verbose than other languages (and more maintenable)! See 'new', and 'abort' keywords for managing dynamic tasks, as well as other specialized packages like Ada.Synchronous_Task_Control.

like image 200
Santiago Avatar answered Feb 15 '23 12:02

Santiago


Ada's terminology for a thread is a "task". Ada doesn't have semaphores (as such) built directly into the language, but Googling for something like "Ada semaphore" should turn up a fair number of hits. AdaPower.com, in particular, has quite a bit about concurrent programming in Ada (and, for that matter, almost all kinds of programming in Ada).

like image 39
Jerry Coffin Avatar answered Feb 15 '23 12:02

Jerry Coffin