http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55471



--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-11-26 
19:23:22 UTC ---

If you change the code to join the threads instead of leaving them running when

the program exits then the output is correct.



#include <iostream>

#include <mutex>

#include <thread>

#include <atomic>



std::mutex m;



int main()

{

    const unsigned N = 10;



    std::atomic<bool> run(true);



    std::thread t[N];



    for(unsigned i = 0; i < N; ++i)

        t[i] = std::thread([&]()

        {

            while(run)

            {

                m.lock();

                std::cout << "[" << std::this_thread::get_id() << "]: ";

                for(unsigned i = 0; i < 10; ++i)

                    std::cout << i;

                std::cout << std::endl;

                m.unlock();

            }

        });



    std::this_thread::sleep_for(std::chrono::seconds(1));

    run = false;

    for(unsigned i = 0; i < N; ++i)

        t[i].join();

}

Reply via email to