《C语言编程实例 —— 函数参数为函数,作为回调函数》

1.

复制代码
typedef int (*GET_VIDEO_FLAG_CALLBACK)(void* param);
typedef struct {
    volatile int8_t pm_lock_ref;
    pthread_mutex_t pm_lock_mutex;
    pthread_t pm_task;
    GET_VIDEO_FLAG_CALLBACK on_get_video_flag;
} power_manager_t;

static power_manager_t power_ctx;

static int on_get_video_flag(void* param) {
    tag_CAT_CONTEXT* cat_ctx = param;

    return cat_ctx->is_have_video;
}
yd_power_manager_init(on_get_video_flag);
int yd_power_manager_init(int (*on_get_video_flag)(void* param)) { int ret = 0; pthread_attr_t attr; memset(&power_ctx, 0, sizeof(power_manager_t)); power_ctx.pm_lock_ref = 0; power_ctx.on_get_video_flag = on_get_video_flag; pthread_mutex_init(&power_ctx.pm_lock_mutex, NULL); pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 0x1000); pthread_create(&power_ctx.pm_task, &attr, run_pm_task, &power_ctx); pthread_attr_destroy(&attr); return ret; } static void* run_pm_task(void* arg) { power_manager_t* power_ctx = (power_manager_t*)arg; if (power_ctx == NULL) { return NULL; } arch_os_queue_t msg_quene_id = get_msg_queque_id(); prctl(PR_SET_NAME, "pm"); pthread_detach(pthread_self()); while (1) {if (power_ctx == NULL) { break; } if (power_ctx->pm_lock_ref == 0 && get_is_battery_powered()) { printf("power down %llu ms\n", arch_os_ms_now()); send_message(msg_quene_id, CAT_MSG_POWER_DOWN_REQ); } } return NULL; } int yd_pm_power_down(void* cat_ctx, int* isOsdupdating) { int ret = 0; uint8_t is_have_video = 0;
   //该函数直接调用,判断返回值
if (power_ctx.on_get_video_flag) { xxxx; } return ret; }
复制代码

 

posted @   一个不知道干嘛的小萌新  阅读(58)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2021-09-29 《开发板 —— rk板卡设备树引脚计算》
2021-09-29 《RV1126 —— 配置adc并读取adc通道上传感器数值》
2021-09-29 《开发板 —— i2c-tools调试i2c设备以及16位I2C读写工具》
点击右上角即可分享
微信分享提示