No announcement yet.

Multithreaded Function Calls

  • Filter
  • Time
  • Show
Clear All
new posts

  • Multithreaded Function Calls

    For our team last year and most possibly this year as well, we will be multithreading (called multitasking in RobotC) our tasks for TeleOp. We know that for most other languages, when calling the same function from multiple threads, a problem may occur when two or more threads call the same function at the same time. To get around this problem, we have cloned the functions for joystick buttons (which we used in almost all the threads) for each of the tasks that we used the function in, for example task drive would use a function called joy1Btn_1 and task otherthing would use a function called joy1Btn_2. Has anyone ever experienced a problem with just using one function in multiple tasks in RobotC? Is cloning the function for each tasks just superfluous?
    Qingyang Chen
    FTC Team 5485 - PRO, Horseheads, NY
    Sponsored by Corning Inc.
    Join our fan page:

  • #2
    Cloning the functions does not alleviate the problem. The problem you are talking about is called a race condition, and it occurs when 2 or more threads attempt to access the same variable. If two or more functions access the same variable, it doesn't matter if they are using the same instance of code, it matters that it is the same variable being assigned to. In desktop programming, the solution to this problem is to use locks on critical variables. On the NXT, our team has found that there is no need to implement these features. The JoystickDriver.c file which comes with RobotC uses multiple tasks without locks on resources. Our code has 4-6 tasks running at once, and we have had no problems at all with race conditions.
    Max Bareiss

    FTC #248 Fatal Error (2009-2013)
    FRC #3142 Aperture (2009-2012)
    FRC #1302 Team Lionheart (2012-2013)
    ZRHS #89 Team K├╝hlschrank (2011-2013)
    ZRAC #40 Catcher in the Skye (2012)
    ISR 12: Umptysquatch 6
    Rowan University Baja SAE

    And mentoring for life.
    11 seasons of FIRST in 6 years, as a student. Many more as a mentor.