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

Does httplib use I/O multiplexing (select, poll, epoll) and support thousands of concurrent connections? #2076

Closed
Mq-b opened this issue Feb 20, 2025 · 6 comments

Comments

@Mq-b
Copy link

Mq-b commented Feb 20, 2025

Hello!

First of all, I want to say that I really like httplib—it's a very user-friendly library. I would like to know whether it utilizes I/O multiplexing techniques such as select, poll, or epoll. Additionally, does it support handling thousands of concurrent client connections efficiently?

Thank you!
Thank you!

Use `select()` instead of `poll()`

@falbrechtskirchinger
Copy link
Contributor

No, each connection is handled in a blocking manner on a dedicated thread (using poll()/select() on a single socket). As a result, you're limited by the size of the thread pool.

@Mq-b
Copy link
Author

Mq-b commented Feb 20, 2025

What if I don't have it set by default?

I mean I didn't use svr.new_task_queue.

@falbrechtskirchinger
Copy link
Contributor

What if I don't have it set by default?

I mean I didn't use svr.new_task_queue.

From the link:

ThreadPool is used as a default task queue, and the default thread count is 8, or std::thread::hardware_concurrency(). You can change it with CPPHTTPLIB_THREAD_POOL_COUNT.

Looking at the source, it's actually std::thread::hardware_concurrency() - 1.

cpp-httplib/httplib.h

Lines 129 to 134 in a4b2c61

#ifndef CPPHTTPLIB_THREAD_POOL_COUNT
#define CPPHTTPLIB_THREAD_POOL_COUNT \
((std::max)(8u, std::thread::hardware_concurrency() > 0 \
? std::thread::hardware_concurrency() - 1 \
: 0))
#endif

@Mq-b
Copy link
Author

Mq-b commented Feb 20, 2025

I know, I mean, creating a new thread every time there's a new connection? Or is there an implementation of a thread pool inside httplib that doesn't create destruction threads all the time?

@falbrechtskirchinger
Copy link
Contributor

(...) is there an implementation of a thread pool inside httplib that doesn't create destruction threads all the time?

Yes, threads are reused. That's the point of the task queue.

cpp-httplib/httplib.h

Lines 779 to 787 in a4b2c61

class ThreadPool final : public TaskQueue {
public:
explicit ThreadPool(size_t n, size_t mqr = 0)
: shutdown_(false), max_queued_requests_(mqr) {
while (n) {
threads_.emplace_back(worker(*this));
n--;
}
}

@Mq-b
Copy link
Author

Mq-b commented Feb 20, 2025

Thanks, as long as threads aren't created and destroyed all the time, this library should be fine for me at the moment.

@yhirose yhirose closed this as completed Feb 20, 2025
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

3 participants