Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: support for simple semaphore #214

Open
ncopa opened this issue Mar 4, 2019 · 1 comment
Open

feature request: support for simple semaphore #214

ncopa opened this issue Mar 4, 2019 · 1 comment

Comments

@ncopa
Copy link

ncopa commented Mar 4, 2019

It would be handy with a simple semaphore module so it is easy to limit number of concurrent running threads, without needing to reimplement it every time.

local condition = require"cqueues.condition"

local semaphore = {}
function semaphore.new(n)
        local self = { cond = condition.new(), count = n or 1 }
        return setmetatable(self, {__index = semaphore})
end

function semaphore.aquire(self)
        while self.count == 0 do
                self.cond:wait()
        end
        self.count = self.count - 1
end

function semaphore.release(self)
        self.count = self.count + 1
        self.cond:signal()
end
@daurnimator
Copy link
Collaborator

I feel like this would be a good sort of thing to distribute as a separate module. There was talk about moving some of the cqueues submodules out to separate rocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants