libco
libco is a coroutine library which is widely used in wechat back-end service. It has been running on tens of thousands of machines since 2013.
libco is Tencent's C/C++ coroutine library powering WeChat's backend since 2013, letting servers handle millions of concurrent connections efficiently by pausing and resuming lightweight tasks instead of spawning large numbers of threads.
libco is a C and C++ library from Tencent that has been used in WeChat's backend systems since 2013, running on tens of thousands of machines. It provides a way to write server-side code that handles many simultaneous tasks efficiently without the complexity of managing large numbers of threads.
The core idea is coroutines, which are units of work that can pause and resume. When one coroutine is waiting on a slow operation like a network request, instead of blocking a thread while waiting, the system can switch to running another coroutine. This makes it possible to handle far more concurrent connections than a traditional multi-thread approach would allow on the same hardware.
Libco offers two ways to use this. You can use a hook mechanism that intercepts standard network calls at the system level, which means existing code written in a straightforward sequential style can be converted to use coroutines with little or no changes to the business logic. Alternatively, you can use the explicit interfaces (co_create, co_resume, co_yield) to create and control coroutines directly with more fine-grained control.
The library also includes a shared-stack mode for cases where you need to support tens of millions of TCP connections on a single machine.
Building the library requires running make or cmake from the source directory. The README is brief and covers only the core description and build steps. Further documentation is limited.
Where it fits
- Convert existing C/C++ server code to use coroutines with minimal changes by enabling libco's hook mechanism on standard network calls.
- Handle millions of concurrent TCP connections on a single machine using libco's shared-stack coroutine mode.
- Build a high-performance C/C++ backend service that scales to high concurrency without the complexity of traditional multi-threading.