startThread()

Starts a function in a new thread.

Synopsis

int startThread(string|function_ptr func [, anytype arg1...]);

int startThread(class object, string|function_ptr func, args ...);

Parameters

Parameter Description
object An object of a Control++ class.
funcName

Name of the callback function ("work function") that is called when data point values change.

Or

The name of a function pointer to the work function (callback function).

arg1, arg2, ... arguments to be transferred

Return value

If the function could not be started, -1 is returned. If the function was started successfully and the thread stops immediately as well as in all other cases startThread() returns the thread identification number ThreadId (>= 0).

Errors

incorrect/invalid argument, for example, if the function to be started does not exist.

Description

Starts the function funcName as a new, parallel thread. Arguments can be transferred to the function with arg1, arg2, ... .

The function to be started can only be void, i.e. may have no return values.

EXAMPLE

Starts "test as a new thread.

main()
{
  startThread("test");
}
void test()
{

//...
}

EXAMPLE

Calls the function "myWorkFunction" and starts it a s a new thread.

main()
{
  startThread("myWorkFunction", TRUE, 3);




  /* Ausgegeben wird "3", "2" und "1". Die Zeit zwischen der Ausgabe ist 1 Sekunde.*/
}
void myWorkFunction(bool bNeedWait, int iIterations)
{
  while(iIterations > 0)
  {
    Debug("Iteration: ",iIterations--);
    if(bNeedWait)
    {
      delay(1);
    }
  }
}

EXAMPLE

In this example the startThread() function starts a static function from a class as well as a non-static function from a class. Both the function "work"and the function workStatic are each started in a thread. Both of the functions set a data point value and output the value.

class DpCon
{
  public void work(string dp, float value)
  {
    dpSet(dp, value);
    DebugTN(value);
  }

  public static void workStatic(string dp, float value)
  {
    dpSet(dp, value);
    DebugTN(value);
  }
};



main()
{

  DpCon threadObj;
  startThread(threadObj,DpCon::work,"ExampleDP_Arg1.",100);
  startThread(DpCon::workStatic,"ExampleDP_Arg2.",200);
}

If you start the threads of the example above in reverse order:

startThread(DpCon::workStatic,"ExampleDP_Arg2.",22);

startThread(threadObj,DpCon::work,"ExampleDP_Arg1.",21);

the start of the thread "work" does not work since the main() function is already executed and the scope ends.

Assignment

Threads

Availability

CTRL