views:

178

answers:

3

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?

+2  A: 

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).

Jerry Coffin
+4  A: 

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.

Santiago
A: 

Semaphores have to be 'constructed' (rather, custom made) usually using 2 files ( files extensions .adb and .ads), sophisticated semaphores may need 3 files (see 'Concurrent and Real-Time Programming in Ada' Alan Burns and Andy Wellings). There are no threads, but rather tasks in Ada.

For synchronisation in Ada using semaphores, you may see an article on my blogspot ! http://3chevrons.blogspot.com/2010/02/semaphores-in-ada.html

I get a feel that you are trying to relate Ada to concurrency in C and/or threads in Python. However, Ada appeals somewhat differently.

Arkapravo