Subject:
|
Spawning threads in NBC
|
Newsgroups:
|
lugnet.robotics.nxt
|
Date:
|
Sun, 28 Jan 2007 23:59:44 GMT
|
Viewed:
|
12625 times
|
| |
| |
Thanks to David Wallace's very thought-provoking (although somewhat distracting)
emails, I think I can put together a simple pseudo NBC opcode which will let you
start a thread in a manner very similar to how it works in NQC. It requires a
couple extra little threads but overall the technique is pretty clean and the
overhead is pretty small.
Here's the code to spawn a thread from the middle of an existing thread:
#include "NXTDefs.h"
thread myThread
// do spawned thread stuff
RotateMotor(OUT_BC, 50, 7200);
endt
thread myThreadReturner
dseg segment
myThreadSpawner_return byte
dseg ends
subret myThreadSpawner_return
endt
thread myThreadSpawner
precedes myThread, myThreadReturner
exit
endt
thread main
PointOut(200, 200, TRUE);
subcall myThreadSpawner, myThreadSpawner_return
// do the rest of main
Loop:
NumOut(0, 0, FALSE, OutputIOTachoCount(OUT_B));
brtst NEQ, Loop, OutputIORunState(OUT_B)
endt
As you can see, the two "real" threads are "main" and "myThread". The other two
threads (xxxSpawner and xxxReturner) are boilerplate (i.e., soon to be
automatically generated by the NBC compiler) threads which let you start a
separate thread from anywhere within another thread.
Here's the way the code will look (hopefully) in NBC once I implement the new
opcode:
#include "NXTDefs.h"
thread myThread
// do spawned thread stuff
RotateMotor(OUT_BC, 50, 7200);
endt
thread main
PointOut(200, 200, TRUE);
start myThread
// do the rest of main
Loop:
NumOut(0, 0, FALSE, OutputIOTachoCount(OUT_B));
brtst NEQ, Loop, OutputIORunState(OUT_B)
endt
Let me know what you think. And thank David for his persistence. :-)
John Hansen
|
|
Message has 3 Replies:
10 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
Active threads in NXT programmable brick
|
|
|
|