Delay required before task invoked?

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Delay required before task invoked?

Post by pjc30943 »

There appears to be an odd phenomena when setting a module-level variable--say, "bvar"--just before calling a task.

If I set bvar to some value, then begin a task immediately, bvar's value fails to propagate into the task. I know this by setting a temp local variable in the task that is set to bvar as soon as the task is run.

However, any delay (even sleep 0.0) between setting bvar and calling the task causes the value to propagate correctly.

Any thoughts on what I might be doing wrong?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Delay required before task invoked?

Post by dkinzer »

pjc30943 wrote:If I set bvar to some value, then begin a task immediately, bvar's value fails to propagate into the task.
When CallTask is invoked, it doesn't actually run the task at all. Rather, it prepares the task to run by initializing the task stack and adding the task to the task list (immediately following the current task, BTW). At that point, control is returned to the caller. The task does not actually run until its turn comes up in the normal stack rotation. In the scenario that you describe, invocation of Sleep() causes the next ready-to-run task to run which will be the task just created. You could achieve the same effect by calling RunTask().

It sounds as though you may be attempting to pass task-specific information to the task (by setting 'bvar'). If this is the case, it would be simpler to invoke the task using a parameter list. For example:

Code: Select all

Dim ts1(1 to 50) as Byte

Sub Main()
  CallTask Task1(5), ts1
End Sub

Sub Task1(ByVal bvar as Integer)
End Sub
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: Delay required before task invoked?

Post by mikep »

This is probably a good time to also remind people about application note AN-210 "Sharing Data between Tasks". One of the fundamental principles on page 2 describes the problem: when using multi-tasking you should not rely on execution order or timing between tasks as you can never tell when a task may run in relationship to others in the system. As Don describes this principle also applies to CallTask - you cannot tell when the task is going to run. Passing the variable by value works and be careful if you pass a variable by reference.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Delay required before task invoked?

Post by dkinzer »

mikep wrote:[...] be careful if you pass a variable [to a task] by reference.
Quite so. That's why the compiler issues a warning if you invoke a task with a parameter that is passed by reference.
- Don Kinzer
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Post by pjc30943 »

This makes sense.
Except that bvar is physically set in main() before the task is called, so execution order is guaranteed.
Therefore unless it takes more than one instruction cycle to load the variable--especially since the task will only run in the next available slot--the variable should be set:

public bvar as single

main()
...
bvar = something
'sleep (0.0) 'optional
callTask task1, task1stack 'by this call, shouldn't bvar be set?
...
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

pjc30943 wrote:bvar is physically set in main() before the task is called
It is true that assigning a value to 'bvar' happens immediately. It is also true that invoking CallTask does not cause the task to run; it only prepares it to run. (Perhaps a better name for it would have been PrepareTask.)

The point is that if you assign a new value to 'bvar' after the CallTask invocation but before the task has actually had a chance to run, it will never see the original value. This sounds precisely like the symptom that you are reporting. If you want the task to run immediately after it is prepared, add a Sleep(0) call right after the CallTask.
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

dkinzer wrote:If you want the task to run immediately after it is prepared, add a Sleep(0) call right after the CallTask.
Is that guaranteed, that the next task in the runnable list is the one created by CallTask? What happens if I have a loop that creates 10 tasks and then sleeps? What happens if a task was waiting and is now runnable - does that task get in first?

Even it is works as we want it to, should we really be dependent on this mechanism? Presumably as you stated previously, RunTask() is a better choice and then after that task has gone through its initialization and enters its main while loop; it should sleep to allow another task to run.
Mike Perks
Don_Kirby
Posts: 341
Joined: 15 October 2006, 3:48 AM
Location: Long Island, New York

Post by Don_Kirby »

Maybe I missed something.

No matter when the task gets executed, or how, the variable is getting set prior to any of it. Let's say that the variable bvar is set to a hardcoded value. The task is then called , and it runs immediately (for whatever reason). Why wouldn't the value in bvar be available to it? I could understand if the task were already running, and bvar was a multibyte variable that had yet to be completely set. But this, as I understand it, is not the case here.

-Don
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Post by pjc30943 »

Exactly Don Kirby. That's what I'm not clear on either...though it could be something silly I myself am doing in implementation; it seems like the problem I'm having is what you describe.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Don_Kirby wrote:Why wouldn't the value in bvar be available to it?
Normally, it will be. Since I've not seen all of the code, I've been making some assumptions about what was being done.

Perhaps it is time to step back and produce a simple test case that illustrates the problem (real or perceived). With that in hand, we can discuss why it should or won't work as was expected.
- Don Kinzer
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

pjc30943 wrote:Exactly Don Kirby. That's what I'm not clear on either...though it could be something silly I myself am doing in implementation; it seems like the problem I'm having is what you describe.
I posted an example of how to create a task and pass in a variable - see my recent Files append.
Mike Perks
Post Reply