使用线程打印全局变量

时间:2015-12-14 07:55:23

标签: c multithreading cygwin pthreads

这是我的代码,我在CodeBlocks上编写脚本,但我正在Cygwin上编译它。当我执行代码时,最终的sec打印出0,我无法理解为什么。我尝试了我所知道的一切。我在高中四年级。此代码采用int变量(#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; } )并使用它来计算电影的重量。

import UIKit

protocol Popup1ViewcontrollerDelegate {
func cancelButtonClicked(sender:Popup1ViewController)
}

class Popup1ViewController: WCViewController {

var delegate : Popup1ViewcontrollerDelegate?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func close(sender: AnyObject) {
    if (self.delegate && self.delegaterespondsToSelector("cancelActionClicked:")) {
        self.delegate?.cancelButtonClicked(self)
    }
}
}

2 个答案:

答案 0 :(得分:4)

你有很多问题。

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()将确保只有主线程死亡,而不是整个过程。

答案 1 :(得分:0)

layer/1000000000将始终为0,因为您的任务会重新定义layer var并且不会修改全局变量。

然后翻译将为1/10000000000 long long int 如果你想避免这种改变:

long long int  layer = frame * 25;

layer = frame * 25;

在所有任务功能中。

另一件事就是用

layer * tempo;
layer/1000000000;

你没有修改任何东西 我想你的意思是:

layer *= tempo;
layer /= 1000000000;

正如l3x已经写过的那样,您至少需要为启动的每项任务调用pthread_join,让main等待每项任务的结束。

相关问题