Page 1 of 4

NXC: wait(1) for safe task (scheduling) control needed?

Posted: 25 Feb 2011, 21:21
by HaWe
hi,
I'm currently extending my subsumption architecture program consisting of up to 20 tasks.
Do I need to insert a
Wait(1)
into each task for a safe task (scheduling) control?
I'm actually curious because if I needed to, I'm afraid of waisting 20*1ms for each time slice scheduling round - (sorry don't know the exact English term)

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 04:24
by mattallen37
I don't really fully understand the question, but I don't think you need to use wait functions so that the NXT doesn't have errors when multitasking. I think that the NXT takes care of making sure that the multitasking resources are used properly. If you are using mutexes, I think wait functions can be used to sort of set priority.

Also, can 20 tasks be running at the same time? That seems like a lot to me. I think I may have heard that 5 is the limit (at one time).

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 04:32
by muntoo
Although 20 tasks is a bit insane... I think you could do it, but I don't know for sure. Just be sure not to go over the firmware's really dumb limit of 256 tasks+function declarations. Maybe this should be a wish for the NBC/NXC Enhanced Firmware - enable more than 256 tasks/functions to be declared.

To answer the OP's question: no, you're not wasting time. The VM will do what it should automatically do - switch to a new task. Just be sure that your waits aren't excessive - Wait(10000) for every task would just freeze your program. If you're worried about wasting a few nanoseconds, you could use Yield().

John also once wrote a very nice post on how to use #pragma task_priority 20 or something. I can't remember anything... I think it was lost with the downfall of NXTasy. Could you explain it again? Please? Image (See? I even went to the trouble of finding the right smiley for the job! ImageImageImage)

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 04:36
by mattallen37
Now how, may I ask, did you learn of the limit? :? I certainly didn't know that, thanks for that oh-so important piece of information :lol:

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 04:48
by muntoo
mattallen37 wrote:Now how, may I ask, did you learn of the limit? :? I certainly didn't know that, thanks for that oh-so important piece of information :lol:
I knew it from before, but here it is in the NXC Guide:
NXC Guide wrote: There are two distinct types of code blocks: tasks and functions. Each type of code block has its own unique features, but they share a common structure. The maximum number of code blocks of both tasks and functions combined is 256.
Doc, does this count as b******izing the quotes? ;)

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 07:13
by m-goldberg
I suggest using Yield() rather than Wait(1).

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 07:16
by mightor
This is from the API dump:

Code: Select all

/**
 * Yield to another task.
 * Make a task yield to another concurrently running task.
 */
inline void Yield() { asm { wait 1 } }
They're one and the same :)

- Xander

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 07:23
by m-goldberg
mightor wrote:They're one and the same :)
Exactly. But 'Yield()' is easier to type and reads better.

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 09:07
by HaWe
thanks guys for your replies, but I didn't manage to find the answer to my TO question - so again, maybe it was not clearly enough:
- I need to have up to 20 tasks, and this amount of tasks I won't change (currently it's 14, but the limit of independent behaviours has not been finally reached, and admittedly not all of them have to run as quick as possible; for the low priority tasks I'm currently using a Wait(20) or a Wait(50).
- but for the very quick calculating tasks (Neural Net, Navigator, BT Sensor-And-Motor-Multiplexer, Speech Recognition) I actually don't want a Wait(1) if avoidable - but nevertheless there must be enough cpu time remaining for the rest.

So again my question:
MUST I FOR SURE insert at least a Wait(1) to ALL task loops (also to the very fast ones) for a safe scheduling or not ?

Re: NXC: wait(1) for safe task (scheduling) control needed?

Posted: 26 Feb 2011, 09:12
by mightor
No, it is not necessary since it's pre-emptive, however, it is better to insert a wait where you know you're going to wait for something anyway.

- Xander