Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   [Q] How are we supposed to use threads in our robot code? (http://www.chiefdelphi.com/forums/showthread.php?t=116573)

pipsqueaker 30-04-2013 18:39

[Q] How are we supposed to use threads in our robot code?
 
Hey, I'm a new programmer, and right now me and one of my friends are involved in porting our robot code to c++. One big thing that's been throwing me off however is c++'s apparent lack of threads. Whenever I google it I get suggestions to use the Boost library, which I have no idea how to use with WPILib. Results are even harder to find because of the clash of terminology (Google keeps confusing forum threads with code threads), so the end result is that I'm really not sure how (or if) I can thread the code.

I'm building off of the SimpleTemplate project, if you wanted to know.

Thanks!

virtuald 30-04-2013 19:10

Re: [Q] How are we supposed to use threads in our robot code?
 
Quote:

Originally Posted by pipsqueaker (Post 1270370)
Hey, I'm a new programmer, and right now me and one of my friends are involved in porting our robot code to c++. One big thing that's been throwing me off however is c++'s apparent lack of threads. Whenever I google it I get suggestions to use the Boost library, which I have no idea how to use with WPILib. Results are even harder to find because of the clash of terminology (Google keeps confusing forum threads with code threads), so the end result is that I'm really not sure how (or if) I can thread the code.

I'm building off of the SimpleTemplate project, if you wanted to know.

Thanks!

Last time I checked (around 1.38), Boost will work on vxWorks. However, compiling it into your robot code will be annoying. [edit] So don't use that on the robot unless you know what you're doing. [/edit]

Use the Task class instead, that allows you to create additional threads using WPILib.

pipsqueaker 30-04-2013 21:43

Re: [Q] How are we supposed to use threads in our robot code?
 
Are you saying that I have to use boost and WPILib's task class? I looked on WPILib's task documentation page , and it doesn't look like I need to. Just looking for clarification.

Also, on the above page, it says the task::run takes the arguments of types FUNCPTR and int. just asking, the fcptr is just the function name without any arguments or parentheses, correct? Also the arguments are of type int, does that mean I can only pass integers?

Sorry for bombarding you with questions but, like I said, I'm new to this and have no coders on the team experienced enough with c++ to help us.

connor.worley 30-04-2013 21:47

Re: [Q] How are we supposed to use threads in our robot code?
 
I would take a look at pthreads.

virtuald 30-04-2013 21:53

Re: [Q] How are we supposed to use threads in our robot code?
 
Quote:

Originally Posted by pipsqueaker (Post 1270504)
Are you saying that I have to use boost and WPILib's task class?

No.

Quote:

Also, on the above page, it says the task::run takes the arguments of types FUNCPTR and int. just asking, the fcptr is just the function name without any arguments or parentheses, correct?
Yes.

Quote:

Also the arguments are of type int, does that mean I can only pass integers?
Sorta. Since it's C++ and it allows you to shoot yourself in the foot, you can technically pass in anything that is the same size as an integer. Be wary of this behavior, as integer sizes may vary on other platforms (such as your PC).

I recommend searching these forums for more information, other people have asked the same questions.

Toa Circuit 30-04-2013 21:55

Re: [Q] How are we supposed to use threads in our robot code?
 
You will never need to use Boost or any external libraries for any multithreading in C++ for FRC, so save yourself some headaches and just WPILib because it works like a charm.

Here's some code that makes a separate thread to flip the state of a relay, which is spawned at TeleopInit, and killed off at DisabledInit.

Code:

class Robot: public SimpleRobot{
  Task secondThread;
  Relay *relay;
 public:
  Robot():
  secondThread("SecondThread",(FUNCPTR) Robot::threadFunction)
  {
  relay=new Relay(1);
  }

  void threadFunction(UINT32 relayPointer){
  Relay *rly=(Relay*)relayPointer;
  while(true){
    rly->Set(Relay::kForward);
    Wait(1);
    rly->Set(Relay::kReverse);
    Wait(1);
  }
  }

  void Disabled(){
  secondThread.Stop();
  }

  void Teleop(){
  secondThread.Start((UINT32) relay);
  }
};

START_ROBOT_CLASS(Robot)
;

See also:
http://www.chiefdelphi.com/forums/sh...ad.php?t=82398

If you need explanation of anything in this post, just refer to that post^

bob.wolff68 08-05-2013 15:51

Re: [Q] How are we supposed to use threads in our robot code?
 
My team produced a class this year which worked quite well and is quite extensible. All the source is on github. I also gave a pretty complete training discussion about how the class works and why in:

http://www.chiefdelphi.com/forums/sh...87#post1232587

If you have questions after reading that, feel free to contact me or post here. or there.

Thanks,
bob


All times are GMT -5. The time now is 12:51.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi