Expand/Shrink

get_thread_id

Definition: atom thread_id = get_thread_id()
Description: Obtain a unique identifier for the currently executing thread.
pwa/p2js: Not supported.
Comments: Can be used to distinguish calls originating in a background worker thread from those made by the main (gui) thread.
Only the main thread should ever update the gui directly.

The returned value bears no resemblance to the thread handles returned by create_thread().
Example:
constant main_thread = get_thread_id(),
         logcs = init_cs()
sequence loggit_stack = {}

global procedure loggit(string text)
-- (this can be called by any code, in any thread)
    if get_thread_id()=main_thread then
        -- show text in gui
    else
        enter_cs(logcs)
        loggit_stack = append(loggit_stack,text)
        leave_cs(logcs)
    end if
end procedure

function timer_cb(Ihandle /*ih*/)
-- (this is only ever invoked by the main thread)
    -- process any stacked background thread loggit()s in a thread-safe way
    enter_cs(logcs)
    sequence ls = loggit_stack
    loggit_stack = {}
    leave_cs(logcs)
    for i=1 to length(ls) do
        loggit(ls[i])
    end for
    return IUP_DEFAULT
end function    
Implementation: See builtins\VM\pThreadN.e (an autoinclude) for details of the actual implementation.
See Also: create_thread