CPPCThreadPool v1.0 A set of C++ classes
to encapsulate the HTTP server APIs on Windows and a pure C++ 17 Thread Pool
One of the class frameworks I developed and published on my web site many years
ago was W3MFC and CThreadPoolServer.
These helped me learn how HTTP and HTTPS worked as well as learn multi-threaded
coding on Windows. A lot has changed in the years since this code was initially
developed and I wanted to develop a new framework which encapsulates the HTTP Server
API's available on Windows. These APIs let you embed an enterprise ready web server
in your C++ Windows application. HTTPServerWrappers is a set of classes to encapsulate
this functionality. To provide good performance you also need to tie this code to
some sort of thread pool architecture. The CPPCThreadPool classes provide this thread
The classes provided are:
HTTPServer::CAutoInit This class provides a RAII wrapper for the SDK functions
HttpInitialize and HttpTerminate.
HTTPServer::CSession This class provides
a RAII wrapper for the SDK HTTP_SERVER_SESSION_ID typedef.
This class provides a RAII wrapper for the SDK HTTP_URL_GROUP_ID typedef.
HTTPServer::CRequestQueue This class provides a RAII wrapper for the SDK request
HTTPServer::CServer This class provides a actual class you
can use in your client code to implement the HTTP server in your application.
CppConcurrency::CFunctionWrapper This class is used by the CThreadPool framework
to implement its internal queue using std::packaged_task<>.
This class provides a thread pool framework written using just pure C++ 17 code.
- Encapsulates all the
HTTP Server v2.0 functionality in a "HTTPServer" C++ namespace
and is implemented in the HTTPServerWrappers.h header file.
- Allows all the complicated HTTP server functionality to be customized using
simple virtual methods in the HTTPServer::CServer class.
- Provides a complete thread pool implementation using standard C++. This
CppConcurrency::CThreadPool class is based on the thread pool implementation in
the Advanced Thread Management Chapter in the book
Concurrency in Action by Anthony Williams. The code also draws inspiration
https://github.com/f-squirrel/thread_pool & http://roar11.com/2016/01/a-platform-independent-thread-pool-using-c14/.
- The thread pool is implemented in the CPPCThreadPool.h header file in just
over 600 lines of code. It demonstrates a lot of C++ features such as: std::thread,
std::thread::hardware_concurrency, std::unique_ptr, std::make_unique, std::shared_ptr,
std::make_shared, std::move, r-value references, std::atomic, std::mutex, std::condition_variable,
std::vector, std::queue, std::dequeue, thread local storage, std::unique_lock,
std::lock_guard, std::this_thread::yield, std::for_each, std::generate_n, std::back_inserter,
std::future, std::bind, std::invoke_result_t & std::packaged_task.
- The thread pool implements optional per thread queues and task stealing,
both unbounded and bounded synchronized queues and submitting tasks to a specific thread in the thread
- The thread pool implements waitable tasks using std::future and
- The thread pool also supports optionally pumping the Windows message
queue in the worker threads when compiled for Windows, custom worker thread
initialization and cleanup and changing the number of threads in the thread
- A complete suite of unit tests are included in the demo application to
test the thread pool classes.
- Unicode enabled and all code compiles cleanly at warning level 4.
- The code is /analyze clean and has been compiled using Clang-Tidy.
- Please note that unlike W3MFC, the HTTPServerWrappers classes does not provide
prebuilt ISAPI or CGI functionality. For this you will need to
build your own functionality on top of the HTTPServerWrappers classes.
The enclosed zip file contains
the HTTPServerWrappers and CPPThreadPool
source code and a VC 2019 console based application with demo implementation of
a HTTP server.
- You are allowed to include the source code in any product (commercial, shareware,
freeware or otherwise) when your product is released in binary form.
- You are allowed to modify the source code in any way you want except you
cannot modify the copyright details at the top of each module.
- If you want to distribute source code with your application, then you are
only allowed to distribute versions released by the author. This is to maintain
a single distribution point for the source code.
- To use the thread pool classes, simply #include "CPPCThreadPool.h"
in the module you want and instantiate a CThreadPool
instance and uses its various methods.
- The HTTPServer class framework uses the Windows HTTP Server APIs built into
Windows. Because this web server is implemented in HTTP.sys, a kernel component
of Windows, it is a shared resource amongst all running applications. To allow
multiple applications to share this resource, you first need to reserve the
URL which you want your web server application to use. This allows multiple
applications to operate as web servers at the same time on your computer. This
reservation process can be done programmatically using the
HttpSetServiceConfiguration API or using the Windows command line utility
An example netsh command line would be:
netsh http add urlacl
This would setup a reservation
for a web server listening on port 80 over HTTP and responding to any requests
which include /MyUri in its HTTP request. The "user" part of the command
line is used to limit what specific Windows accounts can actually open the HTTP
request queue associated with this URL reservation. The application which houses your web
server must be running under this account to allow it to open the request queue
successfully. To list all the current URL reservations setup on your computer
you would use:
netsh http show urlacl
reservation process would be done at installation time of your Windows application.
documentation for further details on the reservation process.
- To use the web server framework classes simply #include "HTTPServerWrappers.h"
in the module you want and implement a class derived from
HTTPServer::CServer and override its HandleRequest
method to customize the HTTP responses your web server should return.
- Once you have your reservation made, you should then be able to start your
web server by passing the string "http://+:80/MyUri" as one of the
values to the HTTPServer::CServer::Start method.
You can provide multiple URLs to the Start method if you want to handle multiple
URLs from your application. The sample application included in the download
passes the command line parameters it receives to this method and waits for
a keypress to shutdown the application.
- The sample application included in the download includes examples on handling
a simple GET request example from in-memory, a GET directory system listing
example, a GET example which returns a response from a file on the file
system and a POST request which simply echoes back the received form request parameters.
- Note that the code has been developed using Visual Studio 2019 and requires
C++ 17 compilation mode and probably will not compile on earlier versions of
- The thread pool code has been developed in standard C++ 17 and should be compilable
on non-Windows platforms although this has not been explicitly tested.
v1.0 (8 November 2020)