What is multithreading?

Top  Previous  Next

Multithreading is an environment where more than one "thread" of execution can take place at the same time. Without support for multithreading, or more than one "thread" of execution, the programmer will be forced to handle everything in one execution context.

Windows is a multithreaded environment in that many simultanous things can take place at the same time: the email client program can receive an email at the same time as the user is using the spreadsheet program and maybe copying a file all at the same time.

 

In a VPL program without multithreading, only one thread of execution is automatically started by the RTCU system software:

 

PROGRAM test;
                         <--- This is where the main thread automatically starts execution.
DebugMsg(message:="Hello from VPL Program");
END_PROGRAM;

 

 

 

With multithreading, however, the VPL program can start additional threads of execution by declaring thread-blocks:  

 

 

THREAD_BLOCK Piper;
                         <--- This is where the thread "Piper" will start execution.
DebugMsg(message:="Hello from Piper!");
END_THREAD_BLOCK;

 

 

PROGRAM test;
VAR
  th : Piper;                <--- This declares an instance of the thread "Piper".
END_VAR;
 
th();                                <--- This is where the thread "Piper" is started.
DebugMsg(message:="Hello from main!");
END_PROGRAM;

 

 

The output of the the multithreaded program above will therefore be:

 

Hello from main!

Hello from piper!

 

**** OR:

 

Hello from piper!

Hello from main!

 

 

The order of the execution cannot be predicted as the two threads execute concurrently.

 

 

Please continue reading the section Why use multithreading?