Navigation: Guide to Examples >====== Thread Model Example ====== | |
The example program discussed here (MT.CLW) can be found in the
\(CLARION ROOT)\EXAMPLES\THREADS folder.
The New Thread Model Test program demonstrates new aspects of the implementation of pre-emptive threads in Clarion 6.0, specifically:
All instances of threaded variables have different addresses in different threads. The compiler generates the code to select the variable corresponding to the currently running thread.
Initialization and cleanup code for threaded variables is executed for every thread. Specifically, the RTL calls to constructors and destructors of CLASSes declared with the THREAD attribute on startup and termination of every running thread
Clarion 6.0 introduces new interfaces to synchronize access to data belonging to one thread from other threads.
Caution!
This example also shows how suspending of threads can potentially be dangerous if done without great care. If a thread is suspended, and you attempt to make that same thread's Window active (by giving it focus) will cause the main thread and the toolbox thread to lockup. This is because Windows sends a message to the child window, but the inter-thread SendMessage is modal. Also, both the frame and toolbox threads become locked if you suspend the currently active MDI child window.
So this example can also teach what not to do when implementing SUSPEND/RESUME.
What this program does
The program has a dummy CODE section, so all the executed code is invoked from the constructor of a global CLASS declared with the THREAD attribute.
The constructor opens the MDI frame window in the main thread. A second thread is started automatically and opens the docked toolbox window. All further threads are started manually by pressing the button on the frame toolbox or from the menu. They open MDI child windows. These windows change their background color in response to the EVENT:Timer event
To start all new threads the program uses a dummy procedure — all code is made from the constructor of the threaded CLASS.
When a new MDI child window starts, it creates a flat checkbox on the docked toolbox. If you press (untick) the checkbox, the thread that has created that checkbox becomes suspended; if you press (tick) the checkbox again, the thread becomes resumed and continues its work. To avoid conflicts in accessing checkboxes from concurrently running threads the program uses a critical section.