From Team Developer SqlWindows Wiki
Jump to: navigation, search

Multithreading (TD 7.1 and up)


Pointer2.png Threads & events: thread synchronization Pointer.png

Applies to TD 7.1 and up.

Multithreading is introduced in TD 7.1 which makes it possible to start multiple threads within TD.
The TD features for threading actually lack support for synchronization.

You might want to start a thread when another thread has finished.
Or you might want to make sure that only one thread is accessing resources (like files) and that other threads wait for the resources being accessed.

The threading features of Windows can be used in TD. WinAPI has a set of functions for multithreading.
To implement thread synchronization, we can utilize these Windows API functions:


These functions are located in Kernel32.dll.

The definition for these functions can be found in the sample provided in this article.
But you need to be aware of a setting which is many not that well known by TD developers.

See screenshot below:


Each DLL definition has the option to set ThreadSafe to YES/NO.
What this means is that when TD calls an external function, it will take this setting into account concerning threading.
When the setting is NO, TD will force that only one thread can call DLL functions at one time.
When the setting is YES, TD will allow multiple threads calling the DLL at the same time.

The DLL used here, Kernel32.dll, is a threadsafe library. So when defining the external definitions for this DLL we need to set ThreadSafe option to YES.

The TD sample provided here has 2 background threads.

  • Thread1 needs 1 second to perform its actions (just a sleep).
  • Thread2 needs 250 milliseconds.

When running these threads without any synchronization, you will see that during thread1 execution, about 4 thread2 actions are performed, as expected.

The sample shows how to sync them, so that thread 2 will only start executing when thread 1 has finished.

The sample has a checkbox to enable/disable the wait functionality to show the difference in output.

The sample can be downloaded here: