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

Buffered queue #72

Open
WebFreak001 opened this issue Nov 2, 2022 · 1 comment
Open

Buffered queue #72

WebFreak001 opened this issue Nov 2, 2022 · 1 comment

Comments

@WebFreak001
Copy link

I have a library which produces many objects rapidly, where I want a pool of threads to pop one and work on it, which takes a while longer than how fast it's generated. Ideally I would like to have a small buffer of elements, so it's possible to append more ahead-of-time, in cases where tasks take a bit of time, but then speed up.

Right now for a queue there seems to be https://github.com/symmetryinvestments/concurrency/blob/334e522f9e23213788083a1363579f6398e0502d/source/concurrency/data/queue/mpsc.d and https://github.com/symmetryinvestments/concurrency/blob/334e522f9e23213788083a1363579f6398e0502d/source/concurrency/data/queue/waitable.d

(although they aren't really documented or mentioned in the README, so I'm not sure if they are usable)

Is there another primitive I could use or is this a new data type entirely?

@skoppe
Copy link
Collaborator

skoppe commented Nov 2, 2022

The mpsc queue we have is unbounded unfortunately. That is typically not what you want, since you don't want the set of queued items to grow unbounded (e.g. produce them faster than consume them).

However, by creating a Serializer, specifically a semaphore serializer, backed up with a stdTaksPool, you can get this behavior.

The serializer is essentially an atomic task counter that blocks when too many tasks are queued. By backing it up with an stdTaskPool the tasks themselves get executed in parallel.

Writing one isn't that difficult, and can be done outside this library to start with. Ping me on slack and I can help you get started.

More importantly though, you need to consider who owns what. Who owns the threadpool and who owns the specific tasks.

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

No branches or pull requests

2 participants