User Tools

Site Tools


mtoverview.htm
Navigation:  Advanced Topics > Thread Model Documentation > Multi-Threading Programming >====== Overview ====== Previous pageReturn to chapter overviewNext page

This topic introduces programming techniques you implement when you program in a preemptive multi-threading environment as introduced in Clarion 6.0. It identifies the type of code and objects that require attention, and also provides code examples for adding thread synchronization to properly handle access to shared non-threaded data.

Throughout this topic there are references to static variables. There are three ways of creating a variable in Clarion such that it is static:

·The variable is declared in Global Data and does not have the THREAD attribute;

·The variable is declared in Module Data and does not have the THREAD attribute;

·The variable is declared in Procedure Data and has the STATIC attribute, but does not have the THREAD attribute;

Static variables are potentially dangerous in a preemptive environment, and should be avoided where possible. Later sections in this topic will describe what to do when they cannot be avoided.

What is a threading model?

A threading model describes how different threads work together under a multi-threading operating system. There are two types of threading models: cooperative threading and preemptive threading.

Under the cooperative threading model an application could have multiple threads 'running' at the same time. But only one of these threads could do anything at any point in time. For another thread (or program) to get a chance to do anything the current thread had to relinquish control of the PC back to the operating system. The operating system would then choose which thread would next have a chance to do something. Thus the threads cooperate with each other and the operating system to give each other a chance to do something. This was easy to program, but it meant that programs could behave badly and take full control of the PC locking out other programs.

When Windows NT arrived so did a new style of threading: Preemptive multi-threading. Now threads could run simultaneously, either on separate CPUs or by having the operating system suspend one thread and start another whenever the operating system wanted. This released the power of multi-processor machines and stopped the problem of badly behaved programs. But it was a much harder paradigm to program with.

Threads Pre-Clarion 6

Clarion for Windows was designed to make programming easy. Therefore the problems of preemptive threading were considered unnecessarily complex for the rewards. Clarion threads were not cooperative by their nature. They were standard Win32 threads with explicit synchronization implemented in the RTL to emulate cooperative behavior.

The emulation of cooperative threads made it easy to use variables that were global in scope (can be used anywhere in your program), but whose contents were different depending on what thread was active; for example a global variable with the THREAD attribute. To do this some mechanism was required to make sure that the contents changed each time a different thread became active. Due to the cooperative nature of Clarion threads the RTL was responsible to swap the contents of the threaded variables every time a thread gained or lost focus.

This made it very easy to program, but added the restriction that only one Clarion thread could be active at any time

Threads in Clarion 6

Clarion 6 now supports the preemptive thread model. To do this, the automatic data swapping that was done by the RTL had to end. In Clarion 6, the operating system determines when a thread receives a CPU time slice. So there is no opportunity for the RTL to swap the contents of variables.

Although Clarion now supports preemptive multi-threading, it does not mean you have to suddenly rewrite all your programs. If you follow the guidelines set out in this paper for avoiding the use of global variables whenever possible and using the synchronization techniques described, you'll be able to deliver new capabilities to your end-users.

mtoverview.htm.txt · Last modified: 2021/04/15 15:57 by 127.0.0.1