I would reorganize your code if I were you. So, there are some tasks that interact with other tasks, now with two tasks. And there is a linked list that is responsible for saving tasks and managing the insertion / deletion of tasks. This is a global object that synchronized should handle.
That is why I advise you to create a protected object and save a list of tasks in it. Protected is usually used for passive objects, where some resource must be processed synchronously. You may have procedures such as insertion, deletion, etc. This ensures that only one creation and deletion will be performed at a time, and that the linked list will not be inconsistent.
Each task should know the tasks of the "partner", which may change when you insert or delete a task. I advise creating an entry in the task that will update its neighbors. When tasks come or go, the protected object will update its neighbors.
In this case, there is no need to refer to the "this" pointer, since the protected object organizes everything. Only an identifier is required that can identify the task (for deletion).
I am trying to write code, but now I do not have a compiler:
task type computer; type computer_ptr is access all computer; task type computer is entry init(id:integer); entry set_neighbor(left,right:computer_ptr); end computer; protected comp_list is procedure insert; -- called by organizer procedure remove(from:integer); -- called by task private type comp_list is array(integer range<>) of computer_ptr; comps:comp_list(1..MAX):=(others=>null); -- or use own structure end comp_list; task body computer is id_:integer; left_n,right_n:computer_ptr:=null; begin accept init(id:integer) do id_:=id; end init; while true loop select accept set_neighbor(left,right:computer_ptr) do left_n:=left;right_n:=right; end set_neighbor; or -- do its work end select; if (some_condition) then comp_list.remove(id_); break; end if; end loop; end task computer; protected body comp_list is procedure insert is p:computer_ptr; begin p:=new computer; -- add to list -> nr; p.all.init(nr); -- call set_neighbor to its left and itself end insert; procedure remove(from: integer) is begin -- remove from list and get its neighbors -- call set_neighbor regarding new ones end remove; end comp_list;
source share