Lua
C++
Edit

CreateSemaphore

This function creates a semaphore object which can be signaled by one thread and waited on by another thread, for thread synchronization.

Syntax

CreateSemaphore()

Returns

Returns a new semaphore object.

Example

local Leadwerks = require "Leadwerks"

local function EntryPoint(extra)
    -- Cast to Semaphore object
    local semaphore = tolua.cast(extra, "Semaphore")

    -- Wait for signaled state
    semaphore:Wait()

    return nil
end

local displays = Leadwerks.GetDisplays()

-- Create a window
local window = Leadwerks.CreateWindow("Ultra Engine", 0, 0, 800, 600, displays[1], Leadwerks.WINDOW_CENTER + Leadwerks.WINDOW_TITLEBAR)

-- Create a semaphore
local semaphore = CreateSemaphore()

-- Create a thread
local thread = Leadwerks.CreateThread(EntryPoint, semaphore)

while not window:Closed() and not window:KeyDown(Leadwerks.KEY_ESCAPE) do
    -- Press space to signal the semaphore
    if window:KeyHit(Leadwerks.KEY_SPACE) then
        semaphore:Signal()
    end

    -- Detect thread finished
    if thread:GetState() == Leadwerks.THREAD_FINISHED then
        Leadwerks.Notify("Thread finished!")
        return 0
    end

    Leadwerks.Sleep(1)
end

return 0
Copyright © 2006-2025 Leadwerks Software.
All rights reserved.
Leadwerks 4 Documentation