Skip to content

Instantly share code, notes, and snippets.

@melvyniandrag
Created January 21, 2017 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melvyniandrag/7c85d76f032776142e00f11afef3cb0c to your computer and use it in GitHub Desktop.
Save melvyniandrag/7c85d76f032776142e00f11afef3cb0c to your computer and use it in GitHub Desktop.
this code doesnt behave as it does in the video https://www.youtube.com/watch?v=4Udi9iAyVBQ.
/*
Compiled with
g++ -std=c++11 threads.cpp -pthreads
on ubuntu 15.04 with g++ 4.8
My strange output is:
> ./a.out
Hello from main!
Hello from thread 25615440
Hello from thread 25615448
Hello from thread 25615456
Hello from thread 25615464
Hello from thread 25615472
Hello from thread 25615480
Hello from thread 25615488
Hello from thread 25615496
Hello from thread 25615504
Hello from thread 25615512
Hello from thread 25615520
Hello from thread 25615528
Hello from thread 25615536
Hello from thread 25615544
Hello from thread 25615552
Hello from thread 25615560
*/
#include <thread>
#include <iostream>
#include <vector>
#include <chrono>
int main(){
std::vector<std::thread> threads;
for (int i = 0; i < 16; ++i){
threads.emplace_back([&]{
//std::this_thread::sleep_for(std::chrono::milliseconds(10 * i));
std::cout << "Hello from thread " << i << std::endl;
});
}
std::cout << "Hello from main!\n";
for (auto & t : threads){
t.join();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment