Go to Post Designing for failure tends to encourage failure. Worrying about failure analysis during the competition can become a self fulfilling prophesy. Break your pots in the fall and learn how to keep them from breaking. - Mike Betts [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 18-01-2009, 19:04
nickmagus's Avatar
nickmagus nickmagus is offline
Registered User
AKA: Nick Clayton
FRC #0008 (Paly Robotics)
Team Role: Programmer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Palo Alto, Califorinia
Posts: 56
nickmagus is an unknown quantity at this point
Send a message via AIM to nickmagus
Threading

While I am familiar with threading and have used it many times I have never done so in C++. Can anyone refer me to a good threading tutorial?
Reply With Quote
  #2   Spotlight this post!  
Unread 18-01-2009, 19:10
Jared Russell's Avatar
Jared Russell Jared Russell is online now
Taking a year (mostly) off
FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy)
Team Role: Engineer
 
Join Date: Nov 2002
Rookie Year: 2001
Location: San Francisco, CA
Posts: 3,071
Jared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond reputeJared Russell has a reputation beyond repute
Re: Threading

For what environment? Threads are a system-dependent feature and can be implemented at various levels of the system.

POSIX is a pretty commonly used threading library on Linux-based systems; you basically call a "create thread" function and pass it a function pointer for whatever code it is that you want to run asynchronously. Primitives like mutexes and semaphores are included.

This is a good overview of POSIX threading.
Reply With Quote
  #3   Spotlight this post!  
Unread 18-01-2009, 19:38
nickmagus's Avatar
nickmagus nickmagus is offline
Registered User
AKA: Nick Clayton
FRC #0008 (Paly Robotics)
Team Role: Programmer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Palo Alto, Califorinia
Posts: 56
nickmagus is an unknown quantity at this point
Send a message via AIM to nickmagus
Re: Threading

for the cRio.
Reply With Quote
  #4   Spotlight this post!  
Unread 18-01-2009, 19:39
dpeterson3 dpeterson3 is offline
Registered User
FRC #1178 (DURT)
Team Role: Alumni
 
Join Date: Aug 2008
Rookie Year: 2008
Location: St. Louis
Posts: 158
dpeterson3 is a jewel in the roughdpeterson3 is a jewel in the roughdpeterson3 is a jewel in the roughdpeterson3 is a jewel in the rough
Re: Threading

Here is another old thread dedicated to it. http://www.chiefdelphi.com/forums/sh...ad.php?t=71138
On the CRIO, they are called tasks. I am not sure how to use them yet myself. That part of the WPI manual is blanked out. Look at the tasks.h file in the WPILib folder. It helps a little. Also look at the camera examples in WindRiver. I believe they are using seperate tasks for image aquisition.

hope this helps a little
Reply With Quote
  #5   Spotlight this post!  
Unread 19-01-2009, 00:32
nickmagus's Avatar
nickmagus nickmagus is offline
Registered User
AKA: Nick Clayton
FRC #0008 (Paly Robotics)
Team Role: Programmer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Palo Alto, Califorinia
Posts: 56
nickmagus is an unknown quantity at this point
Send a message via AIM to nickmagus
Re: Threading

Does anybody have any sample code of a thread (task) that they have got working. At the moment it appears that the task is being deleted.
Reply With Quote
  #6   Spotlight this post!  
Unread 19-01-2009, 08:15
Shinigami2057 Shinigami2057 is offline
Slackware Is Your New God (Mentor)
AKA: Harry Bock
FRC #1350 (Rambots)
Team Role: Programmer
 
Join Date: Oct 2006
Rookie Year: 2006
Location: Johnston, RI
Posts: 106
Shinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really nice
Re: Threading

Hi nickmagus,

I'd recommend you use the WPILib Task class, which is a simple wrapper over the VxWorks task/thread API. Here's some sample code to get you started:

To define a task, you have to give it a name. The task will show up in debugging as FRC_task_name, where task_name is the value passed to the Task constructor. The second argument to the constructor is a FUNCPTR, which is your threading function.
Code:
MyRobot::RobotInit()
{
    Task myTask("image_process", imageProcessThread);

    myTask.Start();
}
The thread function must return an int and take a variable number of arguments (up to 10), which is denoted in C/C++ by "..." as the last function argument.

Your task will run from start to finish once, after Start is called. If you don't want it to be deleted, then put a loop in it so it doesn't return until you're done:
Code:
int imageProcessThread(...)
{
   va_arg argptr;
   // do stuff...
}
To get access to any of your 10 arguments, you must use the va_arg macros, which are explained here:
http://www.cppreference.com/wiki/c/other/va_arg

If you are using shared data between threads, you must be sure to enforce synchronization between the threads. WPILib has an interesting abstraction for synchronization with semaphores - examples are in the C++ Programming Guide - but it's really more confusing in my opinion. PM me if you need help with synchronization.
__________________
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
Reply With Quote
  #7   Spotlight this post!  
Unread 19-01-2009, 14:18
nickmagus's Avatar
nickmagus nickmagus is offline
Registered User
AKA: Nick Clayton
FRC #0008 (Paly Robotics)
Team Role: Programmer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Palo Alto, Califorinia
Posts: 56
nickmagus is an unknown quantity at this point
Send a message via AIM to nickmagus
Re: Threading

Thanks for your help Shinigami. Here's what I have found. If you start a task in an init function it is deleted when the init function ends. It appears the task thinks its a local object but when I try to put the definition as a global I get an error "error: expected `,' or `...' before string constant". Does anybody know how to make it global (I will experiment and post the answer if I figure it out.)
Reply With Quote
  #8   Spotlight this post!  
Unread 01-02-2009, 15:47
Chief Pride's Avatar
Chief Pride Chief Pride is offline
<3
AKA: James Kiefer
no team
 
Join Date: Sep 2007
Rookie Year: 2007
Location: Kalamazoo, MI
Posts: 653
Chief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond repute
Re: Threading

I'm having difficulty getting the small code sample to compile, Shinigami. Hopefully, you or someone else could lend a hand?

Here is what I currently have:
Code:
#include "WPILib.h"

/** TODO: Change this message.
 * This is a demo program showing the use of the RobotBase class.
 * The SimpleRobot class is the base of a robot application that will automatically call your
 * Autonomous and OperatorControl methods at the right time as controlled by the switches on
 * the driver station or the field controls.
 */ 
class Robot : public SimpleRobot
{
	RobotDrive myRobot; 	// robot drive system
	Task CameraThread; 
	

public:
	Robot(void):		// these must be initialized in the same order 
		myRobot(1, 2),	// as they are declared above.
		CameraThread("camera_thread", imageProcessThread())
	{
		GetWatchdog().SetExpiration(0.1);
	}

	/** 
	 * Drive left & right motors for 2 seconds then stop
	 */
	void Autonomous(void)
	{
		GetWatchdog().SetEnabled(false);
		myRobot.Drive(0.5, 0.0); 	// drive forwards half speed
		Wait(2.0); 					// for 2 seconds
		myRobot.Drive(0.0, 0.0); 	// stop robot
	}

	void OperatorControl(void)
	{
		GetWatchdog().SetEnabled(true);
		while (IsOperatorControl())
		{
			GetWatchdog().Feed();	//woof.
		}
	}
	
	int imageProcessThread(...)
	{
	   // do stuff...
		return 1;
	}
};

START_ROBOT_CLASS(SimpleRobot);
Thanks.
Reply With Quote
  #9   Spotlight this post!  
Unread 01-02-2009, 17:58
Shinigami2057 Shinigami2057 is offline
Slackware Is Your New God (Mentor)
AKA: Harry Bock
FRC #1350 (Rambots)
Team Role: Programmer
 
Join Date: Oct 2006
Rookie Year: 2006
Location: Johnston, RI
Posts: 106
Shinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really niceShinigami2057 is just really nice
Re: Threading

Chief Pride,

I can see two small problems with your code, both of which are very easy to fix. The first:

Quote:
Originally Posted by Chief Pride View Post
Code:
		CameraThread("camera_thread", imageProcessThread())
When you pass a function as an argument, you don't use parenthesis after the thread - parenthesis indicate a function call, not the function itself. So it should be just
Code:
CameraThread("camera_thread", imageProcessThread)
Secondly, your task function (imageProcessThread) must be declared outside any class definition. So move it after the class ends and before START_ROBOT_CLASS.
You may also have to put a "prototype" of your function before your class definition to make the compiler happy, like so:

Code:
int imageProcessThread(...); /* prototype */

class Robot : public SimpleRobot {
  // ...
};

int imageProcessThread(...)
{
   return 1;
}

START_ROBOT_CLASS(SimpleRobot);
Good luck and let me know if you have any more trouble.
__________________
One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs.
Reply With Quote
  #10   Spotlight this post!  
Unread 01-02-2009, 21:32
Chief Pride's Avatar
Chief Pride Chief Pride is offline
<3
AKA: James Kiefer
no team
 
Join Date: Sep 2007
Rookie Year: 2007
Location: Kalamazoo, MI
Posts: 653
Chief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond reputeChief Pride has a reputation beyond repute
Re: Threading

Thank you very much, Shinigami.

Although, if you have a minute, would you mind letting me know why the prototype makes the compiler happy?

Thanks.
Reply With Quote
  #11   Spotlight this post!  
Unread 02-02-2009, 18:04
intellec7's Avatar
intellec7 intellec7 is offline
108 programmer
AKA: Gustavo
FRC #0108 (SigmaC@ts)
Team Role: Programmer
 
Join Date: Sep 2005
Rookie Year: 2006
Location: Hollywood, Florida
Posts: 65
intellec7 is on a distinguished road
Send a message via AIM to intellec7 Send a message via MSN to intellec7
Re: Threading

The compiler compiles the source code from top to bottom, so if you have a source file like :

Code:
test_function();

void test_function(void)
{ //do stuff 
}
it is best to prototype "test_function" before you call it. This lets the compiler know that there is a definition of the function. Some compilers might not compile the source code without the prototypes, others might throw a "implicit reference" warning.

Last edited by intellec7 : 02-02-2009 at 18:07. Reason: code tags
Reply With Quote
  #12   Spotlight this post!  
Unread 02-02-2009, 19:23
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,183
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: Threading

There is a good example of how to utilize tasks in WPILib source. Check out StartCameraTask in the AxisCam.cpp file.

Last edited by Tom Bottiglieri : 02-02-2009 at 19:26.
Reply With Quote
  #13   Spotlight this post!  
Unread 05-02-2009, 14:41
byteit101's Avatar
byteit101 byteit101 is offline
WPILib maintainer (WPI)
AKA: Patrick Plenefisch
no team (The Cat Attack (Formerly))
Team Role: Programmer
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Worcester
Posts: 699
byteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of lightbyteit101 is a glorious beacon of light
Re: Threading

here is a brief overview:
Code:
#include...
Task tsk("myTask",MyTask);//creates task "tsk" identified by the name "myTask" on the cRIO, calling function MyTask
//put variables to be accesed by the task and norm. code here
RobotDrive myRobot(1,2);
class ...
...
void Auto()
{
    while (!tsk.IsReady())
    { }
    tsk.Start(1);//start MyFunction on another thread w/ args (1);
    while (IsAuto())
    {
    }
    tsk.Stop();
}
...
...
};//end of class
void MyFunction (int arg1)
{
//code using arg1
}
You can have up to 10 int args, and the function must be global
__________________
Bubble Wrap: programmers rewards
Watchdog.Kill();
printf("Watchdog is Dead, Celebrate!");
How to make a self aware robot: while (∞) cout<<(sqrt(-∞)/-0);
Previously FRC 451 (The Cat Attack)
Now part of the class of 2016 at WPI & helping on WPILib
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Threading on the CRIO dpeterson3 Programming 4 04-01-2009 17:56
Threading tool not working.... strange? q_prof Inventor 3 20-02-2006 00:14
Muti-threading Ryan M. Programming 19 26-04-2004 09:10


All times are GMT -5. The time now is 02:38.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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