Libevent is a nice library for handling and dispatching events, as well as doing nonblocking I/O.
This is fine, except that it is basically single-threaded.
If you have multiple CPUs or a CPU with hyperthreading,
you're really under-utilizing the CPU resources available to your server application.
Because your event pump is running in a single thread and therefore can only use one CPU core at a time.
The solution is to create one libevent event queues (AKA event_base) per active connection,
each with its own event pump thread.
This project does exactly that,
giving you everything you need to write high-performance, multi-threaded, libevent-based socket servers.
There are mentionings of running libevent in a multithreaded implementation,
however it is very difficult (if not impossible) to find working implementations.
This project is a working implementation of a multi-threaded, libevent-based socket server.
Build output server.o
is in build
directory.
You can choose each build method.
Bazel look up src/BUILD
file.
foo@bar:~/multithread-libevent-echo-server$ bazel build //src:server
INFO: Analyzed target //src:server (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //src:server up-to-date:
bazel-bin/src/server
INFO: Elapsed time: 0.121s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
Then, copy executable file.
foo@bar:~/multithread-libevent-echo-server$ cp bazel-bin/src/server build/server.o
foo@bar:~/multithread-libevent-echo-server$ make
Go to src directory.
foo@bar:~/multithread-libevent-echo-server$ cd src
foo@bar:~/multithread-libevent-echo-server$ gcc -o ../build/server.o src/server.c src/workqueue.c -levent -lpthread
foo@bar:~/multithread-libevent-echo-server$ clang-12 -o ../build/server.o src/server.c src/workqueue.c -levent -lpthread
Server running in port defined in server.c [SERVER_PORT]
.
foo@bar:~/multithread-libevent-echo-server/build$./server.o
Server running in [SERVER_PORT]
Build Docker image.
foo@bar:~/multithread-libevent-echo-server/$ podman build -t multithread-event-server .
Run container.
foo@bar:~/multithread-libevent-echo-server/$ podman run -p 8080:8080 --name multithread-event-server multithread-event-server:latest
Server running in [SERVER_PORT]
The server itself simply echoes whatever you send to it.
Start it up, then telnet
foo@bar:~/telnet localhost 8080
Copyright (c) 2012 Ronald Bennett Cemer
This software is licensed under the BSD license.
See the accompanying LICENSE.txt for details.