返回不正确的值与pthreads玩的时候我遇到了一个问题,当我测试的子线程之间的在pthread_join()。我试图避免共享变量,所以我用另一个thread_create()函数来执行真实在pthread_create()。在pthread_join(c)中
为了跟踪线程,我创建了一个pthread_t数组tid [N]来存储线程,并且索引也会根据数组中的索引附加到每个线程。
但是,输出示出了一些螺纹具有相同的索引。 我想变量索引必须以某种方式共享。索引被转移为实际参数到thread_create()和结构变量输入假定的thread_create()函数完成之后被清除。我不太明白为什么索引会在子线程之间共享。 有没有人可以解释这一点?提前致谢!!
总之,
问题1:为什么返回值是不正确的?
问题2:怎样主线程等待其他线程完成(我不知道,所以我用了一段时间(1),以确保所有的线程完成。)
?一个注释:如果我们取消注释第38行中的睡眠(),程序会给出正确的输出。
以下均代码:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4 #include <syscall.h>
5
6 #define N 4
7
8 typedef struct{
9
10 int index;
11 pthread_t *tid;
12
13 }mix;
14
15 void* thread_talk(mix* input)
16 {
17 int* ret;
18 int index = input->index;
19 printf("In process %d, thread %u running, my index is %d\n",
20 getpid(), pthread_self(), index);
21
22 /*thread 0 won't join*/
23 if(input->index == 0){
24 printf("thread 0 will stop\n");
25 return (void*)0;
26 }
27 /*Join the thread based on the index of tid*/
28 pthread_join(input->tid[index - 1], &ret);
29 printf("My index is %d, I receive the return value %d\n",index, ret);
30 return (void*)(input->index);
31 }
32
33 int thread_create(pthread_t* tid, int index)
34 {
35 mix input = {index, tid};
36 printf("my index is %d\n");
37 pthread_create(&tid[index], NULL, thread_talk, (void*)(&input));
38 //sleep(1);
39 return 0;
40 }
41
42 int main()
43 {
44
45 pthread_t tid[N];
46 int i;
47 for(i = 0; i < N; i++)
48 thread_create(tid,i);
49 while(1);
50 return 0;
51 }
''中混合thread_create' input'超出范围的函数返回后,但线程仍在访问它。 – mch
'thread_talk'中的'input-> tid [index-1]'是错误的,它应该只是'input-> tid'。 – zwol
'(void *)(&input)'在线程获得'input'的地址时,input已经*>不见了*。 – ThingyWotsit