lib-zo,C语言另一个协程库,慢操作协程化

lib-zo,C语言另一个协程库,激活文件IO操作协程化

另一个 C 协程库 https://blog.csdn.net/eli960/article/details/146802313

慢操协程化

内部实现是把慢操作放在线程池执行

关键函数

// 如果 zvar_coroutine_block_pthread_count_limit > 0
// 则 block_func(ctx) 在线程池执行, 否则在当前程直接执行
void *zcoroutine_block_do(void *(*block_func)(void *ctx), void *ctx);

例子


// 包含协程相关的头文件
#include "coroutine.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syscall.h>

// 获取当前线程的线程ID
static pid_t my_gettid(void)
{
    return syscall(__NR_gettid);
}

static void *slow_action(void *arg)
{
    /* 做一些不适合在协程环境做的事情:
     *     cpu密集型
     *     无法协程化,而导致阻塞的
     *     阻塞类操作
     *     io频繁切换, 如 操作本地数据库, sqlite3, bdb
     */

    // 打印当前线程的ID
    printf("my pthread:%ld\n", (long)my_gettid());
    // 线程休眠1秒
    sleep(1);
    return 0;
}

static void *one_coroutine(void *arg)
{
    // 循环100次调用 zcoroutine_block_do 执行 slow_action 操作
    for (int i=0;i < 100; i++) {
    	// 阻塞执行, 既所谓的同步操作
    	// 但实际上, 是在线程池执行 slow_action
        zcoroutine_block_do(slow_action, 0);
    }
    return 0;
}

// @brief 定时器退出函数,在一定时间后退出程序
static void *timer_exit(void *arg)
{
    // 线程休眠100秒
    sleep(100);
    // 退出程序,返回状态码1
    exit(1);
    return 0;
}

int main(int argc, char **argv)
{
    int i;
    // 初始化协程基础环境
    zcoroutine_base_init();

    // 设置协程阻塞线程的数量限制为3
    zvar_coroutine_block_pthread_count_limit = 3;

    // 创建10个 one_coroutine 协程
    for (i=0;i<10;i++) {
        zcoroutine_go(one_coroutine, 0, 0);
    }
    // 创建一个 timer_exit 协程
    zcoroutine_go(timer_exit, 0, 0);

    // 打印提示信息,表示程序将在100秒后退出
    printf("exit after 100s\n");
    // 打印提示信息,表示阻塞操作将在工作线程中运行
    printf("block do running in worker pthread\n");
    // 打印提示信息,建议使用 strace 跟踪线程
    printf("strace -p pthrad_id\n");
    // 启动协程调度
    zcoroutine_base_run();
    // 清理协程基础环境
    zcoroutine_base_fini();
    // 主线程休眠1秒
    sleep(1);
    return 0;
}

封装

// zcoroutine_block_XXX 基于 zcoroutine_block_do 机制
int zcoroutine_block_pwrite(int fd, const void *data, int len, long offset);
int zcoroutine_block_write(int fd, const void *data, int len);
long zcoroutine_block_lseek(int fd, long offset, int whence);
int zcoroutine_block_open(const char *pathname, int flags, mode_t mode);
int zcoroutine_block_close(int fd);
int zcoroutine_block_rename(const char *oldpath, const char *newpath);
int zcoroutine_block_unlink(const char *pathname);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值