多线程的一种场景

多线程执行一段程序

void* say_hello(void* args)
{
   int tid = *((int*)args);

    printf("i = %d\n", tid);
    //printf("process yuv %s  h264 %s\n",yuv_input[i].c_str(),h264_output[i].c_str());

}


// 线程的运行函数,函数返回的是函数指针,便于后面作为参数  
    // 定义线程的 id 变量,多个变量使用数组
    pthread_t tids[NUM_THREADS];
    int indexes[NUM_THREADS];// 用数组来保存i的值

    for(int i = 0; i < process_no; ++i)
    {
        indexes[i] = i; //先保存i的值

        //参数依次是:创建的线程id,线程参数,调用的函数,传入的函数参数
        int ret = pthread_create(&tids[i], NULL, say_hello, (void *)&(indexes[i]));
        if (ret != 0)
        {
            std::cout << "pthread_create error: error_code=" << ret << std::endl;
        }
    }
    //等各个线程退出后,进程才结束,否则进程强制结束了,线程可能还没反应过来;
    void *status;
    for( int i = 0; i < process_no; ++i )
    {
        int ret = pthread_join( tids[i], &status ); //主程序join每个线程后取得每个线程的退出信息status
        if( ret != 0 )
        {
            std::cout << "pthread_join error:error_code=" << ret << std::endl;
        }
    }
    //pthread_exit(NULL);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值