Pass integer value through pthread_create

前端 未结 4 776
感动是毒
感动是毒 2020-11-28 14:56

I simply want to pass the value of an integer to a thread.

How can I do that?

I tried:

    int i;
    pthread_t thread_tid[10];
    for(i=0;          


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 15:12

    It better to use of a struct for send more parameters in one :

    struct PARAMS
    {
        int i;
        char c[255];
        float f;
    } params;
    
    pthread_create(&thread_id, NULL, fun, (void*)(¶ms));
    

    then you can cast params to PARAMS* and use of it in pthread routine:

    PARAMS *p = static_cast(params);
    p->i = 5;
    strcpy(p->c, "hello");
    p->f = 45.2;
    

提交回复
热议问题