To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.nxtOpen lugnet.robotics.nxt in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / NXT / 434
433  |  435
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:
  Re: Spawning threads in NBC
 
(...) A dream (soon) becoming true ;o) Philo (18 years ago, 29-Jan-07, to lugnet.robotics.nxt)
  Re: Spawning threads in NBC
 
(...) Is there any plan to create a way to stopped threads that have been spawned in this way (similar to NQC)? (18 years ago, 29-Jan-07, to lugnet.robotics.nxt)
  Re: Spawning threads in NBC
 
(...) I'm trying to leave you alone John, but I haven't been too successful :). (18 years ago, 30-Jan-07, to lugnet.robotics.nxt)

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

 
Verified and Trusted Team of Hackers
16 hours ago
Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR