Thread: Threading
View Single Post
  #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