老师您好,初学多线程,练习时,写了个创建线程并回收的循环过程,但在进行传参时发生了参数没有正确传递的情况。代码如下和结果如下:

#define N 4

typedef struct{

     int index;

     pthread_t *tid;

}mix;

void* thread_talk(void* input_value)

{

     int index = (int)(long)input_value;

     printf("In process %d, thread %u running, my index is %d\r\n",getpid(), (unsigned int)pthread_self(), (int)(long)input_value);

          

     /*Join the thread based on the index of tid*/

     printf("My index is %d, I receive the return value \r\n",index);

     

      return (void*)(&index);

}

int thread_create(mix * input)

{

int ret;

int index = input->index;

     printf("my index is %d\r\n",index);   

     if(input->index > 0)      

     {

     pthread_join(input->tid[input->index - 1], (void *)&ret);

printf(“ret %d\r\n”,ret);

sleep(1);

     }

     pthread_create(&input->tid[input->index], NULL, thread_talk, (void*)(long)index);

     printf("input->tid[input->index] %d\r\n",(unsigned int)input->tid[input->index]);

sleep(1);

     return 0;

}

int main()

{

     pthread_t       tid[N];

     mix input = {0, tid};

     int             i;

     for(i = 0; i < N; i++)

     {

     input.index = i;

thread_create(&input);

     }

     while(1);      

     return 0;

}

输出结果如下:

image

老师,这是什么原因导致的呢?
当我改用地址传参时:如下

void* thread_talk(void* input_value)
{
int index = *(int *)input_value;
printf(“In process %d, thread %u running, my index is %d\r\n”,getpid(), (unsigned int)pthread_self(),index);

     /*Join the thread based on the index of tid*/
     printf("My index is %d, I receive the return value \r\n",index);
     
      return (void*)(&index);

}

int thread_create(mix * input)
{
int ret;
int index = input->index;
printf(“my index is %d\r\n”,index);
if(input->index > 0)
{
pthread_join(input->tid[input->index - 1], (void )&ret);
printf(“ret %d\r\n”,ret);
sleep(1);
}
pthread_create(&input->tid[input->index], NULL, thread_talk, (void
)&index);
printf(“input->tid[input->index] %d\r\n”,(unsigned int)input->tid[input->index]);

sleep(1);
return 0;
}
也出现了传递不对的情况
image

但是以下情况却能得到预期的结果
#define N 4

typedef struct{

     int index;
     pthread_t *tid;

}mix;

void* thread_talk(void* input_value)
{
int* ret;
mix *input = (mix *)input_value;
int index = input->index;
printf(“In process %d, thread %u running, my index is %d\r\n”,getpid(), (unsigned int)pthread_self(), index);

     /*thread 0 won't join*/
     if(input->index == 0){
             printf("thread 0 will stop\r\n");
             return (void*)0;
     }
     /*Join the thread based on the index of tid*/
     pthread_join(input->tid[index - 1], (void *)&ret);
     printf("My index is %d, I receive the return value %d\r\n",index,(int)ret);
      return (void*)(input->index);

}

int thread_create(pthread_t* tid, int index)
{
mix input = {index, tid};
printf(“my index is %d\r\n”,index);
pthread_create(&tid[index], NULL, thread_talk, (void*)(&input));
sleep(1);
return 0;
}

int main()
{

     pthread_t       tid[N];
     int             i;
     for(i = 0; i < N; i++)
             thread_create(tid,i);
     while(1);      
     return 0;

}
image

把 pthread_join 第二个参数修改为 NULL;
我调试下来看起来像是局变量越界了;