Threads

WinCC OA is an event-driven system. If a certain function needs to be executed as a result of an event (for example, a change in value of a DP attribute),then a connection between the required DP attribute and the function to be run must be set up in the CTRL script. This is achieved by the function dpConnect().

As different events, sometimes occurring simultaneously, can trigger different functions, such functions - also known as Callback or Work functions - are executed in parallel in CTRL. One instance of a function processed in this way is called a thread.

A thread is handled independently of other active threads at that time. Each thread has its own data or stack area of memory, i.e. if a certain Callback function - let us call it "myCB" - is connected by dpConnect() to various DP attributes and then executed, sometimes in parallel, when these attributes are changed, then the local variables within "myCB" are only visible or accessible inside a thread, but not between threads.

dpConnect() is not the only means of automatically running or terminating threads by the WinCC OA; the CTRL programmer also has the option to run functions in parallel using startThread()/stopThread(), where the same conditions apply to variables as described above.

Example

In the example a thread function is defined, and then executed in the main() function as a thread.

void myFunction()
{
  int a;
  for (a = 0; a < 10; a++)
    DebugN(getThreadId(), a);
}

main()
{
  startThread("myFunction");
      
/*starts a new thread of the function "myFunction"*/

  startThread("myFunction");
      
/*runs a 2nd thread in parallel, where each thread
                              itself counts from 0 to 9 and outputs the
                              corresponding thread number*/

}

For a function that is always executed as a thread, the only way of saving values in variables that appear again in the next thread, is to use script-global variables, i.e. variables which are declared outside any function, and thus belong to the script and not to a particular thread.

A function which is processed as a standalone thread can never have any return arguments, and so must be void. This is because a calling function simply runs a thread in parallel, so does not wait for it to end. Thus it may be that the calling function has already finished execution while the thread started by this function is still running. This means that a thread is also not able to return values to the calling function.

The function declaration must therefore appear as follows, for instance:

void myFunction()

or in shortform

myFunction()

At least one thread is started in each script, namely the one which executes the main() function.

Functions that are started as a thread only use optional parameters if they are constants. Variables, including constant variables, are not evaluated.

Example

A function using a constant and a variable is called directly and as thread.

const int CCC = 999;
func(int a = 6, int b = CCC)
{
  DebugN(a, b);
}

main()
{
  DebugN("direct call");
  func();
  DebugN("started as thread");
  startThread("func");
}

Output:

["direct call"]
[6][999]
["started as thread"]
[6][0]