coost 是一个现代 C++ 协程库,提供轻量级的协程实现、高性能调度器和丰富的同步原语。它使 C++ 中的异步编程变得更加简洁,同时保持了高性能。
git clone https://github.com/idealvin/coost.git
cd coost
mkdir build && cd build
cmake ..
cmake --build .
find_package(coost REQUIRED)
target_link_libraries(your_target PRIVATE coost::co)
#include "co/co.h"
int main() {
// 启动协程
go([](){
std::cout << "Hello from coroutine!" << std::endl;
});
// 等待所有协程执行完毕
co::sleep(100);
return 0;
}
#include "co/co.h"
int main() {
// 创建多个协程
for (int i = 0; i < 5; i++) {
go([i](){
co::sleep(100 * (i + 1));
std::cout << "Coroutine " << i << " finished" << std::endl;
});
}
// 等待所有协程执行完毕
co::sleep(1000);
return 0;
}
#include "co/co.h"
void producer(co::chan<int>& ch) {
for (int i = 0; i < 5; i++) {
ch << i;
std::cout << "Produced: " << i << std::endl;
co::sleep(100);
}
ch.close();
}
void consumer(co::chan<int>& ch) {
int v;
while (ch >> v) {
std::cout << "Consumed: " << v << std::endl;
}
}
int main() {
co::chan<int> ch(32);
go(producer, ch);
go(consumer, ch);
co::sleep(1000);
return 0;
}
#include "co/co.h"
co::Mutex g_mutex;
int g_counter = 0;
void increment() {
for (int i = 0; i < 100; i++) {
co::MutexGuard g(g_mutex);
g_counter++;
}
}
int main() {
co::WaitGroup wg;
for (int i = 0; i < 10; i++) {
wg.add(1);
go([&wg](){
increment();
wg.done();
});
}
wg.wait();
std::cout << "Counter: " << g_counter << std::endl;
return 0;
}
#include "co/co.h"
void tcp_client() {
auto conn = co::tcp_connect("127.0.0.1", 8888, 3000);
if (!conn) {
std::cout << "Connection failed" << std::endl;
return;
}
// 发送数据
const char* msg = "Hello, Server!";
int n = conn->send(msg, strlen(msg));
std::cout << "Sent: " << n << " bytes" << std::endl;
// 接收数据
char buf[1024];
n = conn->recv(buf, sizeof(buf));
if (n > 0) {
buf[n] = '\0';
std::cout << "Received: " << buf << std::endl;
}
}
int main() {
go(tcp_client);
co::sleep(2000);
return 0;
}
#include "co/co.h"
void handle_connection(int fd) {
char buf[1024];
while (true) {
int n = co::recv(fd, buf, sizeof(buf));
if (n <= 0) break;
buf[n] = '\0';
std::cout << "Received: " << buf << std::endl;
// 回显数据
co::send(fd, buf, n);
}
close(fd);
}
void tcp_server() {
co::tcp_server("127.0.0.1", 8888, handle_connection);
}
int main() {
go(tcp_server);
std::cout << "Server started on port 8888" << std::endl;
co::sleep(60000); // 运行60秒
return 0;
}
#include "co/co.h"
void timer_example() {
for (int i = 0; i < 5; i++) {
std::cout << "Tick: " << i << std::endl;
co::sleep(500); // 睡眠500ms
}
}
void delay_example() {
co::sleep(1000);
std::cout << "Delayed 1 second" << std::endl;
}
int main() {
go(timer_example);
go(delay_example);
co::sleep(3000);
return 0;
}
#include "co/co.h"
void http_server() {
co::tcp_server("0.0.0.0", 8080, [](int fd) {
char buf[4096];
int n = co::recv(fd, buf, sizeof(buf));
if (n <= 0) {
close(fd);
return;
}
std::string response =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Content-Length: 12\r\n"
"\r\n"
"Hello World!";
co::send(fd, response.c_str(), response.length());
close(fd);
});
}
int main() {
go(http_server);
std::cout << "HTTP server started on port 8080" << std::endl;
co::sleep(60000);
return 0;
}
| API | 说明 |
|---|---|
go(f, args...) |
创建并启动协程 |
co::sleep(ms) |
协程睡眠(毫秒) |
co::chan<T> |
协程间通信通道 |
co::Mutex |
协程互斥锁 |
co::WaitGroup |
等待组 |
co::Event |
事件 |
co::tcp_connect |
TCP 客户端连接 |
co::tcp_server |
TCP 服务器 |