A thread is a running macro or function. All functions it calls run in the same thread.


A thread is the basic unit to which the operating system allocates CPU time. It executes code. Each thread runs simultaneously with other threads. Every process (running program) has one or more threads. To create effect of several simultaneously running threads, operating system divides CPU time into time slices (~20 ms). It allocates a CPU time slice to each thread that need it. The currently executing thread is suspended when its time slice elapses, allowing another thread to run. If there are multiple CPU, threads may run simultaneously on different CPU. You can read more in wikipedia.


QM creates new thread for each macro or function started by the user, a trigger, mac or tim. It also creates new thread for menu/toolbar/autotext items. Multiple threads can run simultaneously, except macros without option 'Run simultaneously'. Multiple threads of the same function etc also can run simultaneously.


All currently running threads, except special threads, are displayed in the Running Items list. To see the list, check View Active Items in the Run menu. Private threads are hidden by default. To see threads without opening QM window, use QM tray menu.


See also: EnumQmThreads, IsThreadRunning, EndThread.


Special threads


There are two threads that run all the time:


1. QM main thread. Functions that run in it: global variable constructors/destructors, toolbar hook functions, functions executed during compilation and some other functions. Also, a macro or other application can send message to run a function in the main thread.

2. Thread of filter functions. All filter functions of key, mouse and window triggers run in this thread.


Thread variables of these threads are destroyed when unloading QM file, before destroying global variables.


Threads also can be explicitly or implicitly created by some dll functions, for example CreateThread. Avoid such functions if possible, because then QM cannot destroy thread variables and free some other memory allocated for that thread.


In all these special threads don't use atend.




Before a thread ends:

  1. QM 2.3.4. All top-level windows of that thread receive WM_QM_ENDTHREAD message. They should destroy self (DestroyWindow) and free their allocated data to avoid memory leaks.
  2. Called functions registered with atend.
  3. Destroyed local variables of thread main function.
  4. Destroyed thread variables.