当我运行以下程序时,输出为5.
为什么5?为什么不是8?
void *doit(void *vargp) {
int i = 3;
int *ptr = (int*)vargp;
(*ptr)++;
}
int main() {
int i = 0;
pthread_t tid;
pthread_create(&tid, NULL, doit, (void*)&i);
pthread_join(tid,NULL);
i = i + 4;
printf("%d",i);
}
Run Code Online (Sandbox Code Playgroud)
在doit中,i的值在语句处从0增加到1
(*ptr)++
Run Code Online (Sandbox Code Playgroud)
线程完成后,将其递增4
i = i + 4;
Run Code Online (Sandbox Code Playgroud)
所以价值是5