* Newbie: Task parametering
@ 2005-10-17 0:23 Andrew Price
2005-10-17 0:53 ` jimmaureenrogers
0 siblings, 1 reply; 8+ messages in thread
From: Andrew Price @ 2005-10-17 0:23 UTC (permalink / raw)
Hello all,
I need to initialise some tasks with a few parameters when I instantiate
them. After looking at
http://www.adaic.com/docs/95style/html/sec_6/6-1-3.html it seems the
best way to do this is using an access type discriminant to pass a
record to the tasks.
With that in mind, I created this record type;
type Task_Data is
record
Name : aliased String(1..8);
Period : aliased Duration;
Repetitions : aliased Integer;
end record;
and then in the main part of my program I use it like this;
type Task_Handle is access A_Task; -- used to point to the task
Another_Task : Task_Handle;
type Data_Handle is access Task_Data; -- used to point to its data
Data_For_Task : Data_Handle;
begin -- The main prog will now spawn three of the above tasks
-- Parameterise the first task and then start it.
Data_For_Task := new Task_Data;
Data_For_Task.Name := "Task A ";
Data_For_Task.Period := 0.5;
Data_For_Task.Repetitions := 5;
Another_Task := new A_Task(Data_For_Task);
So while this approach works, I can't help but feel that I'm going about
this in a 'long winded' way (especially coming from a mostly C background).
Is there are neater way of putting the data into the Task_Data records?
Or is there an better approach to the whole task parameterisation I
should consider?
Now is probably a good time to add the disclaimer that this is part of a
lab work exercise for a course I'm currently undertaking at university.
Thanks in advance for any advice,
Andrew.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Newbie: Task parametering
2005-10-17 0:23 Newbie: Task parametering Andrew Price
@ 2005-10-17 0:53 ` jimmaureenrogers
2005-10-17 2:37 ` tmoran
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: jimmaureenrogers @ 2005-10-17 0:53 UTC (permalink / raw)
Andrew Price wrote:
> Hello all,
>
> I need to initialise some tasks with a few parameters when I instantiate
> them. After looking at
> http://www.adaic.com/docs/95style/html/sec_6/6-1-3.html it seems the
> best way to do this is using an access type discriminant to pass a
> record to the tasks.
>
> With that in mind, I created this record type;
>
> type Task_Data is
> record
> Name : aliased String(1..8);
> Period : aliased Duration;
> Repetitions : aliased Integer;
> end record;
>
>
> and then in the main part of my program I use it like this;
>
> type Task_Handle is access A_Task; -- used to point to the task
> Another_Task : Task_Handle;
> type Data_Handle is access Task_Data; -- used to point to its data
> Data_For_Task : Data_Handle;
>
> begin -- The main prog will now spawn three of the above tasks
>
> -- Parameterise the first task and then start it.
> Data_For_Task := new Task_Data;
> Data_For_Task.Name := "Task A ";
> Data_For_Task.Period := 0.5;
> Data_For_Task.Repetitions := 5;
> Another_Task := new A_Task(Data_For_Task);
>
> So while this approach works, I can't help but feel that I'm going about
> this in a 'long winded' way (especially coming from a mostly C background).
>
> Is there are neater way of putting the data into the Task_Data records?
> Or is there an better approach to the whole task parameterisation I
> should consider?
>
> Now is probably a good time to add the disclaimer that this is part of a
> lab work exercise for a course I'm currently undertaking at university.
There is a little roblem with your approach.
It does not actually initialize the task. The task begins running
before the values are set. You are likely to encounter a race
condition with your approach.
True initialization can be achieved by providing a discriminant
to the task.
On the other hand, you may not need a true initialization.
Instead, you may simply want the task to begin by accepting
an entry that passes in all the values you need to set before
the task does anything else.
Read about the concepts of discriminants and task entries.
You should then be able to decide which approach best fits
your needs.
Jim Rogers
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Newbie: Task parametering
2005-10-17 0:53 ` jimmaureenrogers
@ 2005-10-17 2:37 ` tmoran
2005-10-17 4:10 ` tmoran
2005-10-17 4:58 ` Andrew Price
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: tmoran @ 2005-10-17 2:37 UTC (permalink / raw)
> type Task_Data is
> record
> Name : aliased String(1..8);
and
> type Data_Handle is access Task_Data; -- used to point to its data
Things of type Data_Handle point to heap-allocated objects (it says
"is access" not "is access all"), so you don't need any "aliased".
> Data_For_Task := new Task_Data;
> Data_For_Task.Name := "Task A ";
> Data_For_Task.Period := 0.5;
> Data_For_Task.Repetitions := 5;
Data_For_Task := new Task_Data("Task A ", 0.5, 5);
or, more explicitly
Data_For_Task := new Task_Data(
(Name => "Task A ", Period => 0.5, Repetitions => 5);
Besides being shorter, these have the advantage over multiple assignment
statements that the compiler will tell you if you left something out.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Newbie: Task parametering
2005-10-17 2:37 ` tmoran
@ 2005-10-17 4:10 ` tmoran
0 siblings, 0 replies; 8+ messages in thread
From: tmoran @ 2005-10-17 4:10 UTC (permalink / raw)
> Things of type Data_Handle point to heap-allocated objects (it says
> "is access" not "is access all"), so you don't need any "aliased".
Oops, that's not right. You don't need any "aliased" unless you have an
"access all" type which can point to a component of a Task_Data record.
You apparently don't have, and presumably don't need, such a thing, so you
can drop the "aliased"s.
> Another_Task := new A_Task(Data_For_Task);
Yes, should create and start a new A_Task with an access to a Task_Data
record as its discriminant.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Newbie: Task parametering
2005-10-17 0:53 ` jimmaureenrogers
2005-10-17 2:37 ` tmoran
@ 2005-10-17 4:58 ` Andrew Price
2005-10-17 10:02 ` Jacob Sparre Andersen
2005-10-17 15:57 ` Poul-Erik Andreasen
2005-10-17 22:51 ` Stephen Leake
3 siblings, 1 reply; 8+ messages in thread
From: Andrew Price @ 2005-10-17 4:58 UTC (permalink / raw)
jimmaureenrogers@worldnet.att.net wrote:
> Andrew Price wrote:
>
>>Hello all,
>>
>>I need to initialise some tasks with a few parameters when I instantiate
>>them. After looking at
>>http://www.adaic.com/docs/95style/html/sec_6/6-1-3.html it seems the
>>best way to do this is using an access type discriminant to pass a
>>record to the tasks.
>>
>>With that in mind, I created this record type;
>>
>>type Task_Data is
>> record
>> Name : aliased String(1..8);
>> Period : aliased Duration;
>> Repetitions : aliased Integer;
>> end record;
>>
>>
>>and then in the main part of my program I use it like this;
>>
>> type Task_Handle is access A_Task; -- used to point to the task
>> Another_Task : Task_Handle;
>> type Data_Handle is access Task_Data; -- used to point to its data
>> Data_For_Task : Data_Handle;
>>
>>begin -- The main prog will now spawn three of the above tasks
>>
>> -- Parameterise the first task and then start it.
>> Data_For_Task := new Task_Data;
>> Data_For_Task.Name := "Task A ";
>> Data_For_Task.Period := 0.5;
>> Data_For_Task.Repetitions := 5;
>> Another_Task := new A_Task(Data_For_Task);
>>
>>So while this approach works, I can't help but feel that I'm going about
>>this in a 'long winded' way (especially coming from a mostly C background).
>>
>>Is there are neater way of putting the data into the Task_Data records?
>>Or is there an better approach to the whole task parameterisation I
>>should consider?
>>
>>Now is probably a good time to add the disclaimer that this is part of a
>>lab work exercise for a course I'm currently undertaking at university.
>
>
> There is a little roblem with your approach.
>
> It does not actually initialize the task. The task begins running
> before the values are set. You are likely to encounter a race
> condition with your approach.
>
> True initialization can be achieved by providing a discriminant
> to the task.
>
> On the other hand, you may not need a true initialization.
> Instead, you may simply want the task to begin by accepting
> an entry that passes in all the values you need to set before
> the task does anything else.
>
> Read about the concepts of discriminants and task entries.
> You should then be able to decide which approach best fits
> your needs.
>
> Jim Rogers
>
Thanks for the input Jim and others.
I thought that the task would not start running until the statement
Another_Task := new A_Task(Data_For_Task);
which occurs after the values in the Data_For_Task record have been set?
I originally wanted to just use multiple discriminants to initialise the
task however I ran into compilation errors because "discriminants must
have a discrete or access type" and I wanted to include a string among
the initialising variables.
From what I have read it seems that maybe I would be better to make use
of a task entry to set up each task, which I have done and it seems much
neater for me.
Thanks again.
Here is what I ended up doing if anyone is interested (I'm sure there
are probably many other improvements that could be made but I'm much
happier with this than the first effort)
task type A_Task is
entry Start(Name: String; Period : Duration; Repetitions : Integer);
end A_Task;
task body A_Task is -- and this is the task's code
Myname : String(1..8);
Myperiod : Duration;
myrepetitions : integer;
Next_Time : Time := clock;
Iteration : Integer := 0;
begin -- It just diplays the task name and some data
from the discriminant
accept Start(Name: String; Period : Duration; Repetitions :
Integer) do
Myname := Name;
Myperiod := Period;
Myrepetitions := Repetitions;
end;
while Iteration < myRepetitions loop
Display_Lock.Seize;
Put("Hello from ");
Put(Image(Current_Task)); -- from Task_Identification package
Put(" with name ");
Put(myName);
New_Line;
Display_Lock.Release;
Iteration := Iteration + 1;
Next_Time := Next_Time + myPeriod;
delay until Next_Time;
end loop;
exception
when others =>
Put("unexpected exception in task ");
Put_line(Image(Current_Task));
end A_Task;
type Task_Handle is access A_Task; -- used to point to the task
Another_Task : Task_Handle;
begin -- The main prog will now spawn three of the above tasks
-- Start first task
Another_Task := new A_Task;
Set_Priority(30,Another_Task'Identity);
Another_Task.Start(Name => "Task A ", Period => 0.5, Repetitions => 5);
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Newbie: Task parametering
2005-10-17 4:58 ` Andrew Price
@ 2005-10-17 10:02 ` Jacob Sparre Andersen
0 siblings, 0 replies; 8+ messages in thread
From: Jacob Sparre Andersen @ 2005-10-17 10:02 UTC (permalink / raw)
Andrew Price wrote:
>> Andrew Price wrote:
>>> type Task_Handle is access A_Task; -- used to point to the task
>>> Another_Task : Task_Handle;
>>> type Data_Handle is access Task_Data; -- used to point to its data
>>> Data_For_Task : Data_Handle;
>>>
>>>begin -- The main prog will now spawn three of the above tasks
>>>
>>> -- Parameterise the first task and then start it.
>>> Data_For_Task := new Task_Data;
>>> Data_For_Task.Name := "Task A ";
>>> Data_For_Task.Period := 0.5;
>>> Data_For_Task.Repetitions := 5;
>>> Another_Task := new A_Task(Data_For_Task);
> I thought that the task would not start running until the statement
>
> Another_Task := new A_Task(Data_For_Task);
>
> which occurs after the values in the Data_For_Task record have been
> set?
That is also my understanding.
> I originally wanted to just use multiple discriminants to initialise
> the task however I ran into compilation errors because
> "discriminants must have a discrete or access type" and I wanted to
> include a string among the initialising variables.
You would have to pass an access value to a string (constant) instead
of a string. Or maybe an access to your Task_Data type.
Greetings,
Jacob
--
�I'm perfectly happy with my current delusional system.�
-- Mirabel Tanner
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Newbie: Task parametering
2005-10-17 0:53 ` jimmaureenrogers
2005-10-17 2:37 ` tmoran
2005-10-17 4:58 ` Andrew Price
@ 2005-10-17 15:57 ` Poul-Erik Andreasen
2005-10-17 22:51 ` Stephen Leake
3 siblings, 0 replies; 8+ messages in thread
From: Poul-Erik Andreasen @ 2005-10-17 15:57 UTC (permalink / raw)
jimmaureenrogers@worldnet.att.net wrote:
> Andrew Price wrote:
>
>>Hello all,
>>
>>I need to initialise some tasks with a few parameters when I instantiate
>>them. After looking at
>>http://www.adaic.com/docs/95style/html/sec_6/6-1-3.html it seems the
>>best way to do this is using an access type discriminant to pass a
>>record to the tasks.
>>
>>With that in mind, I created this record type;
>>
>>type Task_Data is
>> record
>> Name : aliased String(1..8);
>> Period : aliased Duration;
>> Repetitions : aliased Integer;
>> end record;
>>
>>
>>and then in the main part of my program I use it like this;
>>
>> type Task_Handle is access A_Task; -- used to point to the task
>> Another_Task : Task_Handle;
>> type Data_Handle is access Task_Data; -- used to point to its data
>> Data_For_Task : Data_Handle;
>>
>>begin -- The main prog will now spawn three of the above tasks
>>
>> -- Parameterise the first task and then start it.
>> Data_For_Task := new Task_Data;
>> Data_For_Task.Name := "Task A ";
>> Data_For_Task.Period := 0.5;
>> Data_For_Task.Repetitions := 5;
>> Another_Task := new A_Task(Data_For_Task);
>>
>>So while this approach works, I can't help but feel that I'm going about
>>this in a 'long winded' way (especially coming from a mostly C background).
>>
>>Is there are neater way of putting the data into the Task_Data records?
>>Or is there an better approach to the whole task parameterisation I
>>should consider?
>>
>>Now is probably a good time to add the disclaimer that this is part of a
>>lab work exercise for a course I'm currently undertaking at university.
>
>
> There is a little roblem with your approach.
>
> It does not actually initialize the task. The task begins running
> before the values are set. You are likely to encounter a race
> condition with your approach.
Where do you get that idea. Before any allocation the access_varible
only contain null;
PEA
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Newbie: Task parametering
2005-10-17 0:53 ` jimmaureenrogers
` (2 preceding siblings ...)
2005-10-17 15:57 ` Poul-Erik Andreasen
@ 2005-10-17 22:51 ` Stephen Leake
3 siblings, 0 replies; 8+ messages in thread
From: Stephen Leake @ 2005-10-17 22:51 UTC (permalink / raw)
"jimmaureenrogers@worldnet.att.net" <jimmaureenrogers@worldnet.att.net> writes:
> It does not actually initialize the task. The task begins running
> before the values are set.
This is wrong; the OP is correct.
The task is created by 'new A_Task', and starts running then.
--
-- Stephe
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2005-10-17 22:51 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-17 0:23 Newbie: Task parametering Andrew Price
2005-10-17 0:53 ` jimmaureenrogers
2005-10-17 2:37 ` tmoran
2005-10-17 4:10 ` tmoran
2005-10-17 4:58 ` Andrew Price
2005-10-17 10:02 ` Jacob Sparre Andersen
2005-10-17 15:57 ` Poul-Erik Andreasen
2005-10-17 22:51 ` Stephen Leake
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox