使用线程打印全局变量(Print a global variable using thread)

编程入门 行业动态 更新时间:2024-10-26 15:29:10
使用线程打印全局变量(Print a global variable using thread)

这是我的代码,我在CodeBlocks上编写脚本,但我在Cygwin上编译它。 当我执行代码时,最终的printf打印出0,我无法理解为什么。 我尝试了所有我知道的东西。 我在高中四年级。 此代码采用int变量( sec )并使用它来计算胶片的重量。

#include <stdlib.h> #include <stdio.h> #include <pthread.h> long long int layer=1; void* res_VGA (void* sec){ int tempo = (intptr_t) sec; int res = 640*480; //resolution int frame = res * 3; // the rgb long long int layer = frame * 25; // 25 frame Hz layer * tempo; layer/1000000000; // I need this for getting the gigabyte pthread_exit(0); } void* res_HD (void* sec){ int tempo = (intptr_t) sec; int res = 1080*720; int frame = res * 3; long long int layer = frame * 25; layer * tempo; layer/1000000000; pthread_exit(0); } void* res_FHD (void* sec){ int tempo = (intptr_t) sec; int res = 1920*1080; int frame = res * 3; long long int layer = frame * 25; layer * tempo; layer/1000000000; pthread_exit(0); } int main(){ int sec,res; pthread_t t1; printf("Inserisci il numero di secondi: \n"); scanf("%d",&sec); printf("Seleziona il tipo di risoluzione: \n"); printf("1) VGA \n"); printf("2) HD \n"); printf("3) FHD \n"); do{ scanf("%d",&res); if(res == 1){ pthread_create(&t1, NULL, res_VGA, (void*)(intptr_t)sec); } else if(res == 2){ pthread_create(&t1, NULL, res_HD, (void*)(intptr_t)sec); } else if(res == 3){ pthread_create(&t1, NULL, res_FHD, (void*)(intptr_t)sec); } }while(res != 1 && res != 2 && res != 3); printf("Il film pesa %lld byte", layer/1000000000); return 0; }

This is my code, I'm scripting it on CodeBlocks, but I'm compiling it on Cygwin. When I execute the code, the final printf print me 0, and I can't understand why. I tried everything that I know. I'm in the 4th year of High school. This code take in a int variable (sec) and use it to calculate the weight of a film.

#include <stdlib.h> #include <stdio.h> #include <pthread.h> long long int layer=1; void* res_VGA (void* sec){ int tempo = (intptr_t) sec; int res = 640*480; //resolution int frame = res * 3; // the rgb long long int layer = frame * 25; // 25 frame Hz layer * tempo; layer/1000000000; // I need this for getting the gigabyte pthread_exit(0); } void* res_HD (void* sec){ int tempo = (intptr_t) sec; int res = 1080*720; int frame = res * 3; long long int layer = frame * 25; layer * tempo; layer/1000000000; pthread_exit(0); } void* res_FHD (void* sec){ int tempo = (intptr_t) sec; int res = 1920*1080; int frame = res * 3; long long int layer = frame * 25; layer * tempo; layer/1000000000; pthread_exit(0); } int main(){ int sec,res; pthread_t t1; printf("Inserisci il numero di secondi: \n"); scanf("%d",&sec); printf("Seleziona il tipo di risoluzione: \n"); printf("1) VGA \n"); printf("2) HD \n"); printf("3) FHD \n"); do{ scanf("%d",&res); if(res == 1){ pthread_create(&t1, NULL, res_VGA, (void*)(intptr_t)sec); } else if(res == 2){ pthread_create(&t1, NULL, res_HD, (void*)(intptr_t)sec); } else if(res == 3){ pthread_create(&t1, NULL, res_FHD, (void*)(intptr_t)sec); } }while(res != 1 && res != 2 && res != 3); printf("Il film pesa %lld byte", layer/1000000000); return 0; }

最满意答案

你有很多问题。

1)在这里,

layer * tempo; layer/1000000000; // I need this for getting the gigabyte

你是计算层,但丢弃结果!

你可能想要:

layer = layer * tempo; layer = layer/1000000000;

需要对所有线程进行此更改。

2) layer long long 。 因此最终除法( layer = layer/1000000000 )可能变为零(由于整数除法)。 而是使用long double 。

3)你有一个局部变量long long int layer; 它会影响全局变量。 如果要存储结果(而不是在本地计算值并返回结果),则更改:

long long int layer = frame * 25;

layer = frame * 25;

需要对所有线程函数进行此更改。

4)你的main()线程没有等待其他线程完成。 因此它可能会在另一个线程完成之前退出。 所以你应该调用pthread_exit(0)或pthread_join(t1, 0); 在创建线程后的main() 。

当main()退出时,整个过程就会消失。 pthread_join()会让它等待线程的完成。 pthread_exit()将确保只有主线程死亡,而不是整个过程。

You have multiple problems.

1) Here,

layer * tempo; layer/1000000000; // I need this for getting the gigabyte

You are computing layer but discard the results!

You probably want:

layer = layer * tempo; layer = layer/1000000000;

This change needs to be done for all threads.

2) layer is of long long. So the final division (layer = layer/1000000000) could be become zero (due to integer division). Instead use long double.

3) You have a local variable long long int layer; which shadows the global variable. If you want to store the results (as opposed to computing the value locally and returning the result), then change:

long long int layer = frame * 25;

to

layer = frame * 25;

This change needs to be done for all thread functions.

4) You main() thread is not waiting for the other thread to complete. So it might exit before the other thread completes. So you should either call pthread_exit(0) or pthread_join(t1, 0); in main() after creating the thread.

When the main() exits, the whole process dies. pthread_join() would make it wait for the thread's completion. pthread_exit() will make sure only the main thread dies, not the whole process.

更多推荐

本文发布于:2023-07-26 19:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1280280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   全局变量   Print   thread   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!