首页 > 编程语言 > 详细

C++ 线程传递多个参数

时间:2020-07-23 22:26:36      阅读:108      评论:0      收藏:0      [点我收藏+]

使用多线程处理共享数据  有些情况下需要传递多个参数

定义一个结构体:将这个结构体指针,作为void *形参的实际参数传递. 

函数中需要定义一个mypara类型的结构指针来引用这个参数


struct thread_param
{
  void * pthis = NULL;
  int i = -1;
};
int main()
{
   vector<pthread_t> thread_ids(1);
    for (size_t i = 0; i < 1; ++i)
    {
        struct thread_param tp;
        tp.pthis = (void *)this;
        tp.i = i;
        if (0 != pthread_create(&thread_ids[i], NULL, &do_check_thread_func, (void *)&tp))
        {
            WRITE_LOG(LOG_ERROR, "pthread_create failed in 10018877");
            return -1;
        }
        WRITE_LOG(LOG_DEBUG, "do_check_thread_func create thread successfully, tid: %u", thread_ids[i]);
    }
    for (size_t i = 0; i < 1; ++i)
    {
        pthread_join(thread_ids[i], NULL);
    }

    return 0;
}

void* do_check_thread_func(void *data)
{
    thread_param *tt;
    tt = (struct thread_param*)data;
    void *param = tt->pthis; 
    int i = tt->i;
    myclass* pThis = (myclass*)param;
    WRITE_LOG(LOG_DEBUG, "10018877 do_check_thread_func create thread successfully, i:%d", i);
}

 

C++ 线程传递多个参数

原文:https://www.cnblogs.com/6-er-mi-hou/p/13368257.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!