|Navigation: Language Reference > 13 - Built-in Functions >====== UNLOCKTHREAD (unlock the current execution thread) ======|
The UNLOCKTHREAD statement allows a Clarion program to call 3rd-party code or API procedures that contain their own message loop (like Clarion's ACCEPT loop).
Normally, ACCEPT loops in a Clarion program execute in turn (even in 32-bit programs) so that problems do not arise due to simultaneous access to data. Thread-switching only occurs at an ACCEPT statement, and thus only one thread can execute Clarion code at a time. However, if the currently executing thread calls an external procedure (including API functions) that (for example) opens a window and processes messages until the window closes, then other threads must be allowed to execute (co-operatively) to process their own messages. You do this by calling UNLOCKTHREAD before the external procedure, then LOCKTHREAD after it returns.
Because UNLOCKTHREAD may allow other threads to pre-empt the currently executing thread, it is important that you make NO calls to the Clarion runtime library between UNLOCKTHREAD and its corresponding LOCKTHREAD. This means you must not call any Clarion language procedure. You also must not perform any operations involving a STRING, CSTRING, PSTRING, DECIMAL, or PDECIMAL data types. The one exception is that you may pass a STRING, CSTRING, or PSTRING variable as a RAW parameter to an external (non-Clarion) procedure. Failure to observe this restriction may result in data on another thread becoming corrupted, or other generally unpredictable misbehavior.
The THREADLOCKED() procedure determines whether the thread has been unlocked or not.
UNLOCKTHREAD !Unlock the thread
MyLibraryCodeWithMessageLoop !Call the code that has its own message loop
LOCKTHREAD !Re-lock the thread