简体   繁体   English

线程和全局变量:为什么我得到 10 或 15 作为 output 尽管我不使用变量 y?

[英]Threads and global variables: Why do I get 10 or 15 as output although i don't use the variable y?

I know that both threads can use the global variable k and p and also that after the CPU-time of one thread expired the other thread gets CPU-time and that's why I get different outputs like 9, 10 but I do not understand how the outputs 10 comes from.我知道两个线程都可以使用全局变量kp ,而且在一个线程的 CPU 时间到期后,另一个线程获得 CPU 时间,这就是为什么我得到不同的输出,如 9、10,但我不明白如何输出10来自。 I guess it's because of the variable y although I don't use it.我想这是因为变量y虽然我不使用它。

int k=2;
int* p;
   void t1_f1(void){
   int x=3;
   p=&x;
   sleep(1);
}

void t1_f2(void){
   int y=5;
   k++;
   sleep(1);
}

void* t1_main(void* args){
   t1_f1();
   t1_f2();
   return NULL;
}

void* t2_main(void* args){
   sleep(1);
   k=k* *p;
   printf("%d \n", k);
   return NULL;
}

int main(int argc, char ** argv){
   pthread_t threads[2];
   pthread_create(threads+1, NULL, t2_main, NULL);
   pthread_create(threads, NULL, t1_main, NULL);
   pthread_join(threads[0],NULL);
   pthread_join(threads[1],NULL);
   exit(0);
}

Your program has UB ( Undefined Behavior ) and therefore you cannot expect any consistent output at all.您的程序具有UBUndefined Behavior ),因此您根本不能期望任何一致的 output。

2 Examples for UB in your code: 2 代码中 UB 的示例:

  1. in t1_f1 you assign the global p to the address of a local variable ( x ).t1_f1中,您将全局p分配给局部变量 ( x ) 的地址。 When x goes out of the scope when the function returns, you'll have a dandling pointer, and dereferencing it ( *p ) in t2_main is UB.当 function 返回时x离开 scope 时,您将有一个空指针,并且在t2_main中取消引用它( *p )是 UB。
  2. t2_main might execute before p is initialized at all (it is not initialized at the global scope where it is defined). t2_main可能在p完全初始化之前执行(它没有在定义它的全局 scope 处初始化)。 In this case it's also UB to dereference it.在这种情况下,取消引用它也是 UB。
    Note that as commented above by @AndrewHenle, sleep() is not a threads synchronization call.请注意,正如@AndrewHenle 上面所评论的, sleep()不是线程同步调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我没有得到0、1、2的输出? - Why don't I get 0, 1, 2 as output? 在C中,当我在另一个文件中声明不同数据类型的全局变量时,为什么不出错? - In C, why don't I get an error when I declare an global variable in different data type in an other file? 为什么我不在BSS中获得静态变量? - Why don't I get static variables in the BSS? 我是否应该在一个线程中锁定一个变量,如果我只需要在其他线程中使用它的值,为什么它不工作呢? - Should I lock a variable in one thread if I only need it's value in other threads, and why does it work if I don't? 为什么我不能溢出全局变量? - Why can't I overflow a global variable? 为什么我看不到执行for循环的混合线程 - Why don't I see a mix of threads executing the for loop 做“ int i = 10;”和“ int i; i = 10;”当我是全局变量时执行不同的功能? - Do “int i = 10;” and “int i; i=10;” perform different functions when i is a global variable? 如果我不将辅助变量声明为全局变量,则链接列表不起作用 - Linked list not working if I don't declare the helper variable as global 为什么我不必使用指针? - Why don't I have to use pointers? 为什么我这里没有收到错误? - Why don't I get an error here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM