st_thread_interrupt中断协程介绍—StateThreads协程源码分析

st_thread_interrupt() 函数用于中断一个正在阻塞的协程,例如 协程A 阻塞在 st_cond_wait() 里等待条件变量的通知,或者 阻塞在 st_read() 里面等待网络数据到来。如果你不想等了,就可以用 st_thread_interrupt() 函数来让这个 协程A 停止等待,立即返回,这就是中断。

st_thread_interrupt() 可以中断所有的 阻塞函数

之前《st_cond_wait协程间通信介绍》的 sync 是无法正常退出的,是因为其他 2 个协程一直阻塞在 st_cond_wait() ,所以导致整个线程无法退出。

我们现在就用 st_thread_interrupt() 来中断其他两个协程,让程序可以正常退出。

sync_interrupt.c 的代码如下:

scss 复制代码
#include <stdio.h>
#include <memory.h>
#include "st.h"
#include "../common.h"
st_cond_t *cond_name;
st_mutex_t* mutex_name;
char data[100] = {};

void *publish(void *arg) {
    st_usleep(0.5 * 1000 * 1000LL);

    st_mutex_lock(mutex_name);
    memset(data,0,100);
    strcpy(data,"srs-principle");
    st_utime_t time_now = st_utime();
    printf("Pulish name %s, %lld\r\n",data, time_now);
    st_mutex_unlock(mutex_name);

    st_cond_signal(cond_name);
    return NULL;
}

void *consume(void *arg) {
    int ret;
    printf("Consume start \n");
    ret = st_cond_wait(cond_name);
    printf("st_cond_wait return %d \n",ret);

    st_mutex_lock(mutex_name);
    st_utime_t time_now = st_utime();
    printf("Consume name %s, %lld\r\n",data, time_now);
    st_mutex_unlock(mutex_name);
    return NULL;
}


int main(int argc, char *argv[]) {
    st_init();

    cond_name = st_cond_new();
    mutex_name = st_mutex_new();

    st_thread_t threads[3] = {0};


    st_thread_create(publish, NULL, 0, 0);
    threads[0] = st_thread_create(consume, NULL, 0, 0);
    threads[1] = st_thread_create(consume, NULL, 0, 0);
    threads[2] = st_thread_create(consume, NULL, 0, 0);

    st_usleep(2 * 1000 * 1000LL);

    for (int i = 0; i < 3; ++i) {
        if( threads[i]->state == _ST_ST_COND_WAIT ){
            printf("thread %d is block \n",i );
            st_thread_interrupt(threads[i]);
        }
    }

    st_cond_destroy(cond_name);
    st_mutex_destroy(mutex_name);

    printf("st_thread_exit success \n");
    //退出当前 main 协程
    st_thread_exit(NULL);

    /* 不会运行到这里 */
    return 1;
}

makefile 的修改规则,请参考前面的文章。

运行结果如下:

可以看到程序正常退出了。正常收到通知被唤醒的 st_cond_wait() 返回的是 0 ,而被中断的唤醒的 st_cond_wait() 返回的是 -1


还有一个 st_cond_timedwait() 函数可以只等待指定的时间就立即返回


本文是《 SRS原理 》一书中的文章,如需观看更多内容,请购买本书。

相关推荐
木易士心3 天前
深度解析 Android 音频焦点处理与实战开发
音视频开发
字节跳动视频云技术团队7 天前
豆包视频通话背后,火山引擎重构 Agent 时代多模态传输底座
人工智能·agent·音视频开发
大辉狼_音频架构7 天前
Vol.05 VendorHAL-NXP
嵌入式·音视频开发
字节跳动视频云技术团队8 天前
不止于 4K,火山引擎画质增强让视频从清晰走向细腻
人工智能·音视频开发
字节跳动视频云技术团队10 天前
火山引擎 × 央视网 打造 2026 世界杯沉浸式观赛盛宴
人工智能·音视频开发
码流怪侠10 天前
StreamingBench:首个流式视频理解基准——多模态大模型离人类实时感知还有多远?
llm·github·音视频开发
冯程12 天前
WebRTC 之 AllocationSequence 详解
webrtc·音视频开发
字节跳动视频云技术团队14 天前
Agent 进化论:从对话到协作
人工智能·agent·音视频开发
字节跳动视频云技术团队15 天前
沙发搬到线上:火山引擎视频云如何用RTC+直播打造一场“云上陪看房”?
音视频开发·直播·rtc
字节跳动视频云技术团队15 天前
进球、切片、全网爆:如何打造一座跑赢热搜的赛事“AI短视频工厂”?
人工智能·音视频开发·直播