SRS流媒体服务器03 ---- st-thread

st-thread下载编译

srs使用的是st-thread,下面我们来研究一下。

下载:

  官网:http://sourceforge.net/projects/state-threads/

  git:https://github.com/toffaletti/state-threads

编译:

tar zxvf st-1.9.tar.gz 
cd st-1.9 
make linux-debug

然后会得到obj目录。里面有生成的中间文件*.o, 头文件st.h,libst.so,libst.a和example中的几个例子:lookupdns,proxy,server等,这里注意st.h是动态生成的。

example:

//gcc -I../obj -g huge_threads.c ../obj/libst.a -o huge_threads

//./huge_threads 10000

//./huge_threads 30000

//
#include <stdio.h>
#include "st.h"
#define SLEEP_INTERVAL 30  //in m

void* do_calc(void* arg){
    long pidx = *(long*)arg;
    for(;;){
        printf("sthread [#%ld] usleep
", pidx);
        st_usleep(SLEEP_INTERVAL * 1000);
    }
    return NULL;
}

 

int main(int argc, char** argv){
    if(argc <= 1){
        printf("Test the concurrence of state-threads!
"
            "Usage: %s <sthread_count>
"
            "eg. %s 10000
", argv[0], argv[0]);
        return -1;
    }
    if(st_init() < 0){
        printf("state threads lib runtime init error!");
        reurn -1;
    }

 

    int i, count = atoi(argv[1]);
    for(i = 1; i <= count; i++){
        if(st_thread_create(do_calc, (void*)&i, 0, 0) == NULL){
            printf("create state thread %d failed
", i);
            return -1;
        }
    }
    st_thread_exit(NULL);
    return 0;
}

 编译:

gcc -I../obj -g huge_threads.c ../obj/libst.a  -o huge_threads

运行

./huge_threads 10000

可以看看程序占用的内存和CPU等。

原文地址:https://www.cnblogs.com/vczf/p/13961473.html