令人困惑的C ++线程行为

时间:2016-12-15 07:25:21

标签: c++ multithreading c++11

我正在阅读一些关于C ++ 11线程的文献,并尝试了以下代码:

<project name="org.dita.pdf2w">
    <target name="dita2pdf2w.init">
        <property location="${dita.plugin.org.dita.pdf2w.dir}/cfg" name="customization.dir"/>
    </target>
    <target depends="dita2pdf2w.init, dita2pdf2" name="dita2pdf2w"/>
</project>

输出没有意义,因为我正在运行两个线程,所以每个应该几乎打印在一起,而不是等待一个线程完成启动。相反,每个线程完成然后下一个线程开始,就像以同步方式一样。我在这里缺少什么?

5 个答案:

答案 0 :(得分:2)

它可能是因为创建一个新线程需要一些时间,第一个线程在下一个线程开始之前完成。 你可以选择分离或加入像

这样的线程
 t1.detach();//don't care about t1 finishing 
 or t1.join()//wait for t1 to finish 

答案 1 :(得分:2)

您的操作系统无需同时启动线程;它不需要在不同的核心上启动它们;它不需要为每个线程提供相同的时间。我真的不相信标准要求任何类似的东西,但我没有检查标准,引用正确的部分进行验证。

您可以(无承诺!)通过将代码更改为以下内容来获得所需的行为。这段代码是&#34;鼓励&#34;操作系统为两个线程提供更多时间,并希望允许两个线程在其中一个线程完成之前完全构建。

#include <chrono>                                                
#include <iostream>                                              
#include <thread>                                                

class background_task {                                          
 public:                                                         
  background_task(int val) : data(val), flag(data % 2) {}        
  void operator()() {                                            
    int count = 0;                                               
    while (count < 100) {                                        
      std::this_thread::sleep_for(std::chrono::milliseconds(50));
      if (flag)                                                  
        std::cout << '\n' << data++;                             
      else                                                       
        std::cout << '\n' << data--;                             
      count++;                                                   
    }                                                            
  }                                                              
 private:                                                        
  int data;                                                      
  int flag;                                                      
};                                                               

int main() {                                                     
  std::thread T1{background_task(2)};                            
  std::thread T2{background_task(3)};                            
  T1.join();                                                     
  T2.join();                                                     
  return 0;                                                      
}       

答案 2 :(得分:1)

除了Amir Rasti的回答,我认为值得一提的是调度程序。

如果使用while(1),即使两个线程运行“并行”,您也会看到输出不完全平行。调度程序(操作系统的一部分)将为每个进程提供运行时间,但时间可能会有所不同。因此,在调度程序让另一个进程再次打印之前,一个进程可以打印100个字符。

答案 3 :(得分:1)

尝试以下代码,修改您之前的代码以显示结果:

    TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)
    animation.setDuration(1500); // animation duration
    animation.setRepeatCount(4); // animation repeat count
    animation.setRepeatMode(2); // repeat animation (left to right, right to left)

    animation.setFillAfter(true);
    your_view .startAnimation(animation);//your_view for mine is imageView 

当第二个线程开始时,第一个线程已经完成处理,因此您看到了所看到的内容。

答案 4 :(得分:1)

while(count < 10000)

循环可以在下一个线程开始之前完成,如果你增加循环或在循环中插入一些睡眠,你可以看到差异。