Threads and semaphores in Ada95

自闭症网瘾萝莉.ら 提交于 2019-12-10 20:34:46

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2065136/threads-and-semaphores-in-ada95

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!