Built-in function to enwrap an object for multi-thread use. Such objects can be used from multiple threads without causing a crash.

OutputVar := CriticalObject(Object, lpCriticalSection)
Function Example: obj := CriticalObject(MyCriticalObject)



The name of the variable in which to store the created object.


Existing Object or CriticalObject to use, this can be also a pointer.
When CriticalObject is given, its CriticalSection will be used and second parameter will be ignored.
When this parameter is empty new object will be created and used.


Pointer to a CriticalSection to use. When this parameter is omitted new CriticalSection will be created unless CriticalObject was given in first parameter, then its CriticalSection will be used.

General Remarks

How does it work:

To retrieve the original object from CriticalObject use:

object := CriticalObject(CriticalObject,1)

To retrieve the pointer to CriticalSection use:

lpCriticalSection := CriticalObject(CriticalObject,2)

When last reference to internal object is deleted, CrticalSection is deleted as well.


obj := CriticalObject() ; Create new critical object
Loop, 4 ; Create 4 Threads.
 AhkThread%A_Index% := AhkThread("obj:=CriticalObject(" ObjPtr(obj) ")`nLoop`nobj[" A_Index "]:= A_Index")
Loop ; Show current content of object.
 ToolTip obj.1 "`n" obj.2 "`n" obj.3 "`n" obj.4