THREAD_BLOCK

Top  Previous  Next

This defines the beginning of a thread-block as described in more detail in the section on multithreading

The general structure of a thread-block is:

 

 

THREAD_BLOCK <name>;

VAR_INPUT

 ...

END_VAR;

VAR_OUTPUT

 ...

END_VAR;

 ...

END_THREAD_BLOCK;

 

The syntax is therefore the same as a function-block but with the word THREAD_BLOCK used instead of FUNCTION_BLOCK.

Also the ALIGN and ASYNC attributes are not allowed on a thread-block.

 

 

Advise:

When using threads, it is advised to ensure that all threads will periodically block waiting for some event like a Channel, Semaphore, Mutex, and/or including a Sleep operation. A thread that runs constantly we leave less processing power for other threads.

 

 

Also see the implicit member variable _running.

 

 

 

Example:

INCLUDE rtcu.inc
 
THREAD_BLOCK Piper;
 
DebugMsg(message:="Hello from Piper!");
 
END_THREAD_BLOCK;

 

 

PROGRAM test;
VAR
  th : Piper;
END_VAR;
 
th();
WHILE th._running DO
  Sleep(delay:=100);
END_WHILE;
DebugMsg(message:="Piper has terminated");
END_PROGRAM;