c ++ 11中的多线程?

时间:2014-01-06 06:24:06

标签: c++ multithreading c++11

好的我正在使用Mac学习c ++ 11中的多线程。据我所知,所有线程都是同时执行的。我从here

找到了以下代码
// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread

void foo() 
{
    std::cout << "\nIn foo \n";
}

void bar(int x)
{
    std::cout << "\nIn bar \n";
}

int main() 
{
    std::thread first (foo);     // spawn new thread that calls foo()
    std::thread second (bar,0);  // spawn new thread that calls bar(0)

    std::cout << "main, foo and bar now execute concurrently...\n";

    // synchronize threads:
     first.join();                // pauses until first finishes
    second.join();               // pauses until second finishes

    std::cout << "foo and bar completed.\n";

    return 0;
} 

每次运行代码时,我都会得到奇怪的结果,如以下示例

  

     

aIIinnn,bf aofroo o

     

和bar现在同时执行... foo和bar完成。

我错过了什么?

3 个答案:

答案 0 :(得分:4)

您正在通过std::cout一次从两个主题进行打印。虽然这显然不是数据竞争,因此“安全”[*],但它的输出 - 正如你所看到的 - 并不是特别有意义。您可能希望使用std::mutex来限制访问,或使用提供更强保证的IO库。

除非您致电std::cout.sync_with_stdio(false)

,否则

[*]

答案 1 :(得分:3)

这是因为两个线程同时在标准输出std :: out上写入。您将需要添加线程同步机制(互斥,信号量等)以确保一次只有一个线程写入std :: out。您面临的问题是由于线程彼此平行,这就是它们的目的。搜索互斥锁或信号量并将它们集成到您的代码中,您将解决问题。

如果您多次运行当前代码,您将看到每次执行代码时结果都会有所不同,或者至少在大多数情况下都会有所不同。

答案 2 :(得分:2)

代码分为两部分。第一部分是thread firstthread secondmain同时运行(即同时)。因此,"\n In foo \n""\n In bar \n""main, foo and bar now execute concurrently...\n"的打印将同时发生,因此输出将是它们的随机混合(更正式地称为racing

换句话说,第一部分打印OP中提到的“随机输出”,即"\n In foo \n""\n In bar \n""main, foo and bar now execute concurrently...\n"的随机播放。

在第一部分之后,主线程将暂停,直到第一个线程和第二个线程完成(称为synchronization。)因此,"foo and bar completed.\n"的打印将始终生成准确的输出。