LINUX.ORG.RU

История изменений

Исправление fsb4000, (текущая версия) :

Лол!

// 1.cpp
#include <iostream>
#include <thread>
#include <mutex>
#include <sstream>
#include <vector>

using namespace std;

static void print_cout (int id)
{
    cout << "cout hello from " << id << '\n';
}



auto main() noexcept -> int
{
    try
    {
        vector<thread> v;

        for (size_t i {0}; i < 10; ++i)
        {
            v.emplace_back (print_cout, i);
        }

        for (auto& t : v)
        {
            t.join();
        }
    }
    catch (const exception& e)
    {
        cerr << "Exception: " << e.what() << endl;
    }
    catch (...)
    {
        cerr << "Unknown exception :(" << endl;
    }
}
g++-7 1.cpp -std=c++1z -O3 -pthread
./a.out 
cout hello from cout hello from 1
0cout hello from 
3cout hello from 4

cout hello from 2
cout hello from 5
cout hello from 6
cout hello from 7
cout hello from 8
cout hello from 9

Исходная версия fsb4000, :

Лол!

// 1.cpp
#include <iostream>
#include <thread>
#include <mutex>
#include <sstream>
#include <vector>

using namespace std;

static void print_cout (int id)
{
    cout << "cout hello from " << id << '\n';
}



auto main() noexcept -> int
{
    try
    {
        vector<thread> v;

        for (size_t i {0}; i < 10; ++i)
        {
            v.emplace_back (print_cout, i);
        }

        for (auto& t : v)
        {
            t.join();
        }
    }
    catch (const exception& e)
    {
        cerr << "Exception: " << e.what() << endl;
    }
    catch (...)
    {
        cerr << "Unknown exception :(" << endl;
    }
}
g++-7 1.cpp -std=c++1z -O3 -pthread

./a.out cout hello from cout hello from 1 0cout hello from 3cout hello from 4

cout hello from 2 cout hello from 5 cout hello from 6 cout hello from 7 cout hello from 8 cout hello from 9